Konigsberg Bridge Problem

91
Konigsberg Bridge Problem A river Pregel flows around the island Keniphof and then divides into two. Four land areas A, B, C, D have this river on their borders. The four lands are connected by 7 bridges a – g. Determine whether it’s possible to walk across all the bridges exactly once in returning back to the starting land area.

description

Konigsberg Bridge Problem. A river Pregel flows around the island Keniphof and then divides into two. Four land areas A, B, C, D have this river on their borders. The four lands are connected by 7 bridges a – g. - PowerPoint PPT Presentation

Transcript of Konigsberg Bridge Problem

Page 1: Konigsberg Bridge Problem

Konigsberg Bridge Problem

• A river Pregel flows around the island Keniphof and then divides into two.

• Four land areas A, B, C, D have this river on their borders.

• The four lands are connected by 7 bridges a – g.

• Determine whether it’s possible to walk across all the bridges exactly once in returning back to the starting land area.

Page 2: Konigsberg Bridge Problem

Konigsberg Bridge Problem (Cont.)

AKneiphof

a b

c d gC

D

B f

e

a b

c dg

e

f

A

B

C

D

Page 3: Konigsberg Bridge Problem

Euler’s Graph• Define the degree of a vertex to be the

number of edges incident to it• Euler showed that there is a walk

starting at any vertex, going through each edge exactly once and terminating at the start vertex iff the degree of each vertex is even. This walk is called Eulerian.

• No Eulerian walk of the Konigsberg bridge problem since all four vertices are of odd edges.

Page 4: Konigsberg Bridge Problem

Application of Graphs• Analysis of electrical circuits• Finding shortest routes• Project planning• Identification of chemical compounds• Statistical mechanics• Genertics• Cybernetics• Linguistics• Social Sciences, and so on …

Page 5: Konigsberg Bridge Problem

Definition of A Graph• A graph, G, consists tof two sets, V and E.

– V is a finite, nonempty set of vertices.– E is set of pairs of vertices called edges.

• The vertices of a graph G can be represented as V(G).

• Likewise, the edges of a graph, G, can be represented as E(G).

• Graphs can be either undirected graphs or directed graphs.

• For a undirected graph, a pair of vertices (u, v) or (v, u) represent the same edge.

• For a directed graph, a directed pair <u, v> has u as the tail and the v as the head. Therefore, <u, v> and <v, u> represent different edges.

Page 6: Konigsberg Bridge Problem

Three Sample Graphs

0

3

1 2

0

1

3 4

2

5 6

0

1

2

(a) G1 (b) G2 (c) G3

V(G1) = {0, 1, 2, 3}E(G1) = {(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)}

V(G2) = {0, 1, 2, 3, 4, 5, 6}E(G2) = {(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)}

V(G3) = {0, 1, 2}

E(G3) = {<0, 1>, <1, 0>, <1, 2>}

Page 7: Konigsberg Bridge Problem

Graph Restrictions• A graph may not have an edge

from a vertex back to itself. – (v, v) or <v, v> are called self edge or

self loop. If a graph with self edges, it is called a graph with self edges.

• A graph man not have multiple occurrences of the same edge.– If without this restriction, it is called a

multigraph.

Page 8: Konigsberg Bridge Problem

Complete Graph• The number of distinct unordered pairs

(u, v) with u≠v in a graph with n vertices is n(n-1)/2.

• A complete unordered graph is an unordered graph with exactly n(n-1)/2 edges.

• A complete directed graph is a directed graph with exactly n(n-1) edges.

Page 9: Konigsberg Bridge Problem

Examples of Graphlike Structures

0

2

1 1

2

3

0

(a) Graph with a self edge

(b) Multigraph

Page 10: Konigsberg Bridge Problem

Graph Edges• If (u, v) is an edge in E(G), vertices

u and v are adjacent and the edge (u, v) is the incident on vertices u and v.

• For a directed graph, <u, v> indicates u is adjacent to v and v is adjacent from u.

Page 11: Konigsberg Bridge Problem

Subgraph and Path• Subgraph: A subgraph of G is a graph G’ such that V(G’) V(G) and E(G’) E(G).• Path: A path from vertex u to vertex v in graph G is a

sequence of vertices u, i1, i2, …, ik, v, such that (u, i1), (i1, i2), …, (ik, v) are edges in E(G).– The length of a path is the number of edges on it.– A simple path is a path in which all vertices except possibly

the first and last are distinct.– A path (0, 1), (1, 3), (3, 2) can be written as 0, 1, 3, 2.

• Cycle: A cycle is a simple path in which the first and last vertices are the same.

• Similar definitions of path and cycle can be applied to directed graphs.

Page 12: Konigsberg Bridge Problem

G1 and G3 Subgraphs0

3

1 20

3

1 20

1 2

0 0

1

2

0

1

2

0

1

2

(a) Some subgraphs of G1

(a) Some subgraphs of G3

(i) (ii) (iii) (iv)

(i) (ii)

(iii) (iv)

Page 13: Konigsberg Bridge Problem

Connected Graph• Two vertices u and v are connected in an

undirected graph iff there is a path from u to v (and v to u).

• An undirected graph is connected iff for every pair of distinct vertices u and v in V(G) there is a path from u to v in G.

• A connected component of an undirected is a maximal connected subgraph.

• A tree is a connected acyclic graph.

Page 14: Konigsberg Bridge Problem

Strongly Connected Graph

• A directed graph G is strongly connected iff for every pair of distinct vertices u and v in V(G), there is directed path from u to v and also from v to u.

• A strongly connected component is a maximal subgraph that is strongly connected.

Page 15: Konigsberg Bridge Problem

Graphs with Two Connected Components

0

3

1 2

0

3

1 2

G4

H1H2

Page 16: Konigsberg Bridge Problem

Strongly Connected Components of G3

0

1 2

Page 17: Konigsberg Bridge Problem

Degree of A Vertex• Degree of a vertex: The degree of a vertex is the

number of edges incident to that vertex.• If G is a directed graph, then we define

– in-degree of a vertex: is the number of edges for which vertex is the head.

– out-degree of a vertex: is the number of edges for which the vertex is the tail.

• For a graph G with n vertices and e edges, if di is the degree of a vertex i in G, then the number of edges of G is

1

0

2/)(n

iide

Page 18: Konigsberg Bridge Problem

Abstract of Data Type Graphs

class Graph{// objects: A nonempty set of vertices and a set of undirected edges// where each edge is a pair of verticespublic:

Graph(); // Create an empty graphvoid InsertVertex(Vertex v);void InsertEdge(Vertex u, Vertex v);void DeleteVertex(Vertex v);void DeleteEdge(Vertex u, Vertex v);

Boolean IsEmpty(); // if graph has no vertices return TRUE

List<List> Adjacent(Vertex v);// return a list of all vertices that are adjacent to v

};

Page 19: Konigsberg Bridge Problem

Adjacent Matrix• Let G(V, E) be a graph with n vertices, n ≥ 1.

The adjacency matrix of G is a two-dimensional nxn array, A.– A[i][j] = 1 iff the edge (i, j) is in E(G).– The adjacency matrix for a undirected graph is

symmetric, it may not be the case for a directed graph.

• For an undirected graph the degree of any vertex i is its row sum.

• For a directed graph, the row sum is the out-degree and the column sum is the in-degree.

Page 20: Konigsberg Bridge Problem

Adjacency Matrices

0111101111011110

3210

3210

000101010

210

210

0100000010100000010100000010000000000110000010010000100100000110

76543210

76543210

(a) G1 (b) G3 (c) G4

Vi Vj then A[i,j]=1

i

j

In-degree

Out-degree

Page 21: Konigsberg Bridge Problem

Adjacency Lists• Instead of using a matrix to represent the adjacency of a

graph, we can use n linked lists to represent the n rows of the adjacency matrix.

• Each node in the linked list contains two fields: data and link.– data: contain the indices of vertices adjacent to a vertex i.– Each list has a head node.

• For an undirected graph with n vertices and e edges, we need n head nodes and 2e list nodes.

• The degree of any vertex may be determined by counting the number nodes in its adjacency list.

• The number of edges in G can be determined in O(n + e).• For a directed graph (also called digraph),

– the out-degree of any vertex can be determined by counting the number of nodes in its adjacency list.

– the in-degree of any vertex can be obtained by keeping another set of lists called inverse adjacency lists.

Page 22: Konigsberg Bridge Problem

Adjacent Lists

3210

1331

2 00 00 02 0

[0][1][2][3]

0

1 02 0 0

[0][1][2]

HeadNodes

HeadNodes

(a) G1

(b) G3

u v

Page 23: Konigsberg Bridge Problem

Adjacent Lists (Cont.)

2301

1 00 03 01 0

[0][1][2][3]

HeadNodes

(c) G4

5 0656 0

4 07 0

[4][5][6][7]

Page 24: Konigsberg Bridge Problem

Sequential Representation of Graph

G4

9 11 13 15 17 18 20 22 23 2 1 3 0 0 3 1 2 5 6 4 5 7 60 1 2 3 4 5 6 7 8 9 1

011

12

13

14

15

16

17

18

19

20 21

22

n2e

n+2e+1

Starting points of the lists

Page 25: Konigsberg Bridge Problem

Inverse Adjacency Lists for G3

1 0

0 0

1 0

[0]

[1]

[2]

In-degree

List of adjacent from

Page 26: Konigsberg Bridge Problem

Multilists• In the adjacency-list representation of

an undirected graph, each edge (u, v) is represented by two entries.

• Multilists: To be able to determine the second entry for a particular edge and mark that edge as having been examined, we use a structure called multilists.– Each edge is represented by one node.– Each node will be in two lists.

Page 27: Konigsberg Bridge Problem

Orthogonal List Representation for G3

0

1

2 0

1 0 0

0

0 1 0 0

1 2

1 2 0 0

head nodes (shown twice)

u

v

Out-degree

In-degree

Page 28: Konigsberg Bridge Problem

Adjacency Multilists for G1

[0][1][2][3]

0 1 N1 N3

0 2 N2 N3

0 3 0 N4

1 2 N4 N5

1 3 0 N5

2 3 0 0

N0

N1

N2

N3

N4

N5

HeadNodes

edge (0, 1)edge (0, 2

edge (0, 3)edge (1, 2)edge (1, 3)edge (2, 3)

The lists areVertex 0: N0 -> N1 -> N2Vertex 1: N0 -> N3 -> N4Vertex 2: N1 -> N3 -> N5Vertex 3: N2 -> N4 -> N5

Page 29: Konigsberg Bridge Problem

Weighted Edges• Very often the edges of a graph

have weights associated with them.– distance from one vertex to another– cost of going from one vertex to an

adjacent vertex.– To represent weight, we need

additional field, weight, in each entry.– A graph with weighted edges is called

a network.

Page 30: Konigsberg Bridge Problem

Graph Operations• A general operation on a graph G

is to visit all vertices in G that are reachable from a vertex v.– Depth-first search– Breath-first search

Page 31: Konigsberg Bridge Problem

Depth-First Search• Starting from vertex, an unvisited vertex w

adjacent to v is selected and a depth-first search from w is initiated.

• When the search operation has reached a vertex u such that all its adjacent vertices have been visited, we back up to the last vertex visited that has an unvisited vertex w adjacent to it and initiate a depth-first search from w again.

• The above process repeats until no unvisited vertex can be reached from any of the visited vertices.

Page 32: Konigsberg Bridge Problem

Graph G and Its Adjacency Lists

0

7

1

3 4

2

5 6

10011223

0235

07070707

4

0406

5 06

[0][1][2][3][4][5][6][7

HeadNodes

DFS(0)=0 1 3 7 4 5 2 6

Page 33: Konigsberg Bridge Problem

Analysis of DFS• If G is represented by its

adjacency lists, the DFS time complexity is O(e).

• If G is represented by its adjacency matrix, then the time complexity to complete DFS is O(n2).

Page 34: Konigsberg Bridge Problem

Breath-First Search• Starting from a vertex v, visit all unvisited

vertices adjacent to vertex v.• Unvisited vertices adjacent to these newly

visited vertices are then visited, and so on.• If an adjacency matrix is used, the BFS

complexity is O(n2).• If adjacency lists are used, the time

complexity of BFS is d1+d2+…+dn=O(e).

Page 35: Konigsberg Bridge Problem

Graph G and Its Adjacency Lists

0

7

1

3 4

2

5 6

10011223

0235

07070707

4

0406

5 06

[0][1][2][3][4][5][6][7

HeadNodes

BFS(0)=0 1 2 3 4 5 6 7

Page 36: Konigsberg Bridge Problem

• Determine of G is connected– call DFS or BFS– If there exist unvisited nodes then

unconnected

Page 37: Konigsberg Bridge Problem

Spanning Tree• Any tree consisting solely of edges in G and

including all vertices in G is called a spanning tree.• Spanning tree can be obtained by using either a

depth-first or a breath-first search.• When a nontree edge (v, w) is introduced into

any spanning tree T, a cycle is formed.• A spanning tree is a minimal subgraph, G’, of G

such that V(G’) = V(G), and G’ is connected. (Minimal subgraph is defined as one with the fewest number of edges).

• Any connected graph with n vertices must have at least n-1 edges, and all connected graphs with n – 1 edges are trees. Therefore, a spanning tree has n – 1 edges.

Page 38: Konigsberg Bridge Problem

T: edges used during traversal, also called tree edges

N: nontree edges

Spanning tree: all vertices + T

When G is connected, DFS or BFS is applied, then the edges is partitioned into T and N

Page 39: Konigsberg Bridge Problem

A Complete Graph and Three of Its Spanning Trees

Page 40: Konigsberg Bridge Problem

Depth-First and Breath-First Spanning Trees

3 4

1 2

5 6

0

7

3 4

1 2

5 6

0

7

(a) DFS (0) spanning tree

(b) BFS (0) spanning tree

Page 41: Konigsberg Bridge Problem

3 4

1 2

5 6

0

7

3 4

1 2

5 6

0

7

(a) DFS (0) spanning tree

(b) BFS (0) spanning tree

When a nontree edge (v,w) is introduced into any spanning tree T, then a cycle is formed

cycle

Page 42: Konigsberg Bridge Problem

Biconnected Components

• Definition: A vertex v of G is an articulation point iff the deletion of v, together with the deletion of all edges incident to v, leaves behind a graph that has at least two connected components.

• Definition: A biconnected graph is a connected graph that has no articulation points.

• Definition: A biconnected component of a connected graph G is a maximal biconnected subgraph H of G. By maximal, we mean that G contains no other subgraph that is both biconnected and properly contains H.

Page 43: Konigsberg Bridge Problem

A Connected Graph and Its Biconnected

Components

0

1

4

2 3

8

7

6

5

9

1

4

2 3

7

6

5

0

1

3 5

8

7 7

9

(a) A connected graph(b) Its biconnected components

Maximal without articulation point

Page 44: Konigsberg Bridge Problem

Biconnected Components (Cont.)

• Two biconnected components of the same graph can have at most one vertex in common.

• No edge can be in two or more biconnected components.• The biconnected components of G partition the edges of

G.• The biconnected components of a connected, undirected

graph G can be found by using any depth-first spanning tree of G.

• A nontree edge (u, v) is a back edge with respect to a spanning tree T iff either u is an ancestor of v or v is an ancestor of u.

• A nontree edge that is not back edge is called a cross edge.• No graph can have cross edges with respect to any of its

depth-first spanning trees.

Page 45: Konigsberg Bridge Problem

Biconnected Components (Cont.)

• The root of the depth-first spanning tree is an articulation point iff it has at least two children.

• Any other vertex u is an articulation point iff it has at least one child, w, such that it is not possible to reach an ancestor of u using a path composed solely of w, descendants of w, and a single back edge. ( w 必經之 vertex)

Page 46: Konigsberg Bridge Problem

• Define low(w) as the lowest depth-first number that can be reached from w using a path of descendants followed by, at most, one back edge.

edge}}back a is )( | min{ }, of child a is |)(min{),(min{)(

w, xdfn(x)wxxlowwdfnwlow

Use descendent’s back edge

Use a back edge connecting with w

Page 47: Konigsberg Bridge Problem

Depth-First Spanning Tree

0

1

4

2 3

8

7

6

5

9

1

2

3

4

5

6

7

8

910

3

4

2

1

0

6

7

8

5

9

1

2

3

4

5

6

7

8

9 10

Page 48: Konigsberg Bridge Problem

dfn and low values for the Spanning Tree

vertex 0 1 2 3 4 5 6 7 8 9dfn 5 4 3 1 2 6 7 8 10 9low 5 1 1 1 1 6 6 6 10 9

Min{4,5,dfn(3)=1}Min{8,9,dfn(5)=6}

Min{7,min{6,10,9}}=min{7,6}: use descendent back edge

Articulation points: 1, 4, 5, 7 where it has children w, low(w) >=dfn(w)

Page 49: Konigsberg Bridge Problem

• u is an articulation point iff u is either the root of the spanning tree and has two or more children or u is not the root and u has a child w such that low(w) ≥ dfn(u).

implying this descendent w cannot use another way going back to the parent of u

Page 50: Konigsberg Bridge Problem

Minimal Cost Spanning Tree

• The cost of a spanning tree of a weighted, undirected graph is the sum of the costs (weights) of the edges in the spanning tree.

• A minimum-cost spanning tree is a spanning tree of least cost.

• Three greedy-method algorithms available to obtain a minimum-cost spanning tree of a connected, undirected graph.– Kruskal’s algorithm– Prim’s algorithm– Sollin’s algorithm

Page 51: Konigsberg Bridge Problem

Kruskal’s Algorithm• Kruskal’s algorithm builds a minimum-cost

spanning tree T by adding edges to T one at a time.

• The algorithm selects the edges for inclusion in T in nondecreasing order of their cost.

• An edge is added to T if it does not form a cycle with the edges that are already in T.

• Theorem 6.1: Let G be any undirected, connected graph. Kruskal’s algorithm generates a minimum-cost spanning tree.

Page 52: Konigsberg Bridge Problem

Stages in Kruskal’s Algorithm

0

5

1

6

43

2

10

28

14 16

1218

22

25

0

5

1

6

43

2

0

5

1

6

43

2

10

(a) (b) (c)

24

Start from edges of smallest cost which would not cause cycleUntil n-1 edges

Page 53: Konigsberg Bridge Problem

Stages in Kruskal’s Algorithm (Cont.)

0

5

1

6

43

2

10

0

5

1

6

43

2

(d) (e) (f)

12

10

12

14

0

5

1

6

43

2

10

12

14 16

Page 54: Konigsberg Bridge Problem

Stages in Kruskal’s Algorithm (Cont.)

0

5

1

6

43

2

1014 16

12

22

(g)

0

5

1

6

43

2

1014 16

12

22

(g)

25

Page 55: Konigsberg Bridge Problem

Prim’s Algorithm• Similar to Kruskal’s algorithm, Prim’s

algorithm constructs the minimum-cost spanning tree edge by edge.

• The difference between Prim’s algorithm and Kruskal’s algorithm is that the set of selected edges forms a tree at all times when using Prim’s algorithm while a forest is formed when using Kruskal’s algorithm.

• In Prim’s algorithm, a least-cost edge (u, v) is added to T such that T∪ {(u, v)} is also a tree. This repeats until T contains n-1 edges.

• Prim’s algorithm in program 6.7 has a time complexity O(n2).

Page 56: Konigsberg Bridge Problem

Stages in Prim’s Alogrithm

0

5

1

6

43

2

(a)

10

0

5

1

6

43

2

(b)

10

25

0

5

1

6

43

2

(c)

10

25

22

Include the minimal edges which would form a tree until n-1 edges 找會形成 tree 的 edges 中有 minimal cost

Page 57: Konigsberg Bridge Problem

Stages in Prim’s Alogrithm (Cont.)

0

5

1

6

43

2

(d)

10

25

22

12

0

5

1

6

43

2

(e)

10

25

22

12

16

0

5

1

6

43

2

(f)

10

25

22

12

1614

Page 58: Konigsberg Bridge Problem

Sollin’s Algorithm• Contrast to Kruskal’s and Prim’s algorithms,

Sollin’s algorithm selects multiple edges at each stage.

• At the beginning, the selected edges and all the n vertices form a spanning forest.

• During each stage, an minimum-cost edge is selected for each tree in the forest.

• It’s possible that two trees in the forest to select the same edge. Only one should be used.

• Also, it’s possible that the graph has multiple edges with the same cost. So, two trees may select two different edges that connect them together. Again, only one should be retained.

Page 59: Konigsberg Bridge Problem

Stages in Sollin’s Algorithm

0

5

1

6

43

2

1014

12

22

(a)

0

5

1

6

43

2

(b)

10

25

22

12

1614

1:Chose the minimal edges which form forests2: from the forests choose the minimal edges

Page 60: Konigsberg Bridge Problem

Shortest Paths• Usually, the highway structure can be

represented by graphs with vertices representing cities and edges representing sections of highways.

• Edges may be assigned weights to represent the distance or the average driving time between two cities connected by a highway.

• Often, for most drivers, it is desirable to find the shortest path from the originating city to the destination city.

Page 61: Konigsberg Bridge Problem

Single Source/All Destinations: Nonnegative

Edge Costs• Let S denotes the set of vertices to which the

shortest paths have already been found.1) If the next shortest path is to vertex u, then the

path begins at v, ends at u, and goes through only vertices that are in S. (if we have S, and u is chosen for added, then v u would not go through points not in S)

2) The destination of the next path generated must be the vertex u that has the minimum distance among all vertices not in S.

3) The vertex u selected in 2) becomes a member of S.• The algorithm is first given by Edsger Dijkstra.

Therefore, it’s sometimes called Dijstra Algorithm.

Page 62: Konigsberg Bridge Problem

Single Source/All Destinations: Nonnegative

Edge Costs• Start from the source v0, list out all the vertex

connecting with the source (S={v0})• Choose the next as the one with minimal distance v1

and include it in S (S={s0, s1}) (Since if we have S, and u is chosen for added, then v u would not go through points not in S) 故只由 S 中展開

• Expand the adjacency to vertexes of s1, and compute the total distance

• From the expanded vertexes choose the one with minimal distance

• Repeat until all the vertexes are included

Page 63: Konigsberg Bridge Problem

Graph and Shortest Paths From Vertex 0 to all destinations

0

3 4

1 2

5

10 20

50

15

20

10

35

30

315

Path Length

1) 0, 3

2) 0, 3, 4

3) 0, 3, 4, 1

4) 0, 2

10

25

45

45

(a) Graph (b) Shortest paths from 0

Page 64: Konigsberg Bridge Problem

Diagram for Example 6.5

0

1 23

4

5

67

300

800

17001000

1000

1400

1200

1500

1000 250

900

Los Angeles

San Francisco

Denver

New Orleans Miami

New York

BostonChicago

0017001000001400900010000

25001500001200

080010000300

0

76543210

76543210

Length-adjacencymatrix

Assume that the source is Boston

Page 65: Konigsberg Bridge Problem

Action of Shortest Pathiteration

S Vertex selected

DistanceLA SF DEN CHI BOST NY MIA NO[0] [1] [2] [3] [4] [5] [6] [7]

Initial -- --- +∞ +∞ +∞ 1500 0 250 +∞ +∞

1 {4} 5 +∞ +∞ +∞ 1250 0 250 1150

1650

2 {4,5} 6 +∞ +∞ +∞ 1250 0 250 1150

1650

3 {4,5,6} 3 +∞ +∞ 2450 1250 0 250 1150

1650

4 {4,5,6,3} 7 3350 +∞ 2450 1250 0 250 1150

1650

5 {4,5,6,3,7}

2 3350 3250 2450 1250 0 250 1150

1650

6 {4,5,6,3,7,2}

1 3350 3250 2450 1250 0 250 1150

1650

{4,5,6,3,7,2,1}

Source: Boston 4

Page 66: Konigsberg Bridge Problem

Single Source/All Destinations: General

Weights• When negative edge lengths are permitted, the graph must

not have cycles of negative length.• When there are no cycles of negative length, there is a

shortest path between any two vertices of an n-vertex graph that has at most n-1 edges on it.

1. If the shortest path from v to u with at most k, k > 1, edges has no more than k – 1 edges, then distk[u] = distk-1[u].

2. If the shortest path from v to u with at most k, k > 1, edges has exactly k edges, then it is comprised of a shortest path from v to some vertex j followed by the edge <j, u>. The path from v to j has k – 1 edges, and its length is distk-1[j].

• The distance can be computed in recurrence by the following:

• The algorithm is also referred to as the Bellman and Ford Algorithm.

]}}][[][{min],[min{][ 11 uilengthidistudistudist k

i

kk

Page 67: Konigsberg Bridge Problem

Single Source/All Destinations: General

Weights• Start from the source, list out all the distances

directly connecting with source• Based on the results computing for all u

• repeat n-1 times (since each node has at most n-1 edges)

]}}][[][{min],[min{][ 11 uilengthidistudistudist k

i

kk For <i,u>

According to the propagation, if v to u has at most k-1 edges, then after k-1 iterations, distance of u convergesdistk[u] = distk-1[u

Page 68: Konigsberg Bridge Problem

Directed Graphs

0 1 2

5

7 -5

0 1 2

-2

1 1

(a) Directed graph with a negative-length edge

(b) Directed graph with a cycle of negative length

Page 69: Konigsberg Bridge Problem

Shortest Paths with Negative Edge Lengths

0

1

2

3

4

5

6

(a) A directed graph

k distk[7]

0 1 2 3 4 5 6

1 0 6 5 5 ∞ ∞ ∞

2 0 3 3 5 5 4 ∞

3 0 1 3 5 2 4 7

4 0 1 3 5 0 4 5

5 0 1 3 5 0 4 3

6 0 1 3 5 0 4 3

(b) distk

6-1

1-25

5 -2 -1

3

3

Page 70: Konigsberg Bridge Problem

All-Pairs Shortest Paths• In all-pairs shortest-path problem, we

are to find the shortest paths between all pairs of vertices u and v, u ≠ v.– Use n independent single-source/all-

destination problems using each of the n vertices of G as a source vertex. Its complexity is O(n3) (or O(n2 logn + ne) if Fibonacci heaps are used).

– On graphs with negative edges the run time will be O(n4). if adjacency matrices are used and O(n2e) if adjacency lists are used.

Page 71: Konigsberg Bridge Problem

All-Pairs Shortest Paths (Cont.)

• A simpler algorithm with complexity O(n3) is available. It works faster when G has edges with negative length, as long as the graphs have at least c*n edges for some suitable constant c.– An-1[i][j]: the length of the shortest i-to-j path in G– Ak[i][j]: the length of the shortest path from I to j going through

no intermediate vertex of index greater than k.– A-1[i][j]: is just the length[i][j]

1. The shortest path from i to j going through no vertex with index greater than k does not go through the vertex with index k. so its length is Ak-1[i][j].

2. The shortest path goes through vertex k. The path consists of subpath from i to k and another one from k to j.

Ak[i][j] = min{Ak-1[i][j], Ak-1[i][k]+ Ak-1[k][j] }, k ≥ 0

Page 72: Konigsberg Bridge Problem

Example for All-Pairs Shortest-Paths Problem

A-1-1 0 1 20 0 4 111 6 0 22 3 ∞ 0

A00 0 1 20 0 4 111 6 0 22 3 7 0

A11 0 1 20 0 4 6

1 6 0 22 3 7 0

2

0 1

6

2

4

11

3 A22 0 1 20 0 4 61 5 0 22 3 7 0

(b) A-1 (c) A0

(d) A1 (e) A2

i0j

i{0,1}j

Page 73: Konigsberg Bridge Problem

Transitive Closure• Definition: The transitive closure matrix,

denoted A+, of a graph G, is a matrix such that A+[i][j] = 1 if there is a path of length > 0 fromi to j; otherwise, A*[i][j] = 0.

• Definition: The reflexive transitive closure matrix, denoted A*, of a graph G, is a matrix such that A*[i][j] = 1 if there is a path of length 0 from i to j; otherwise, A*[i][j] = 0.

Page 74: Konigsberg Bridge Problem

Graph G and Its Adjacency Matrix A, A+, A*

1110011100111001110011110

43210

43210

0000010000010000010000010

43210

43210

0 1 2 3 4

(a) Digraph G

1110011100111001111011111

43210

43210

(b) Adjacency matrix A

(c) A+(d) A* =A+ +{=0 path}

Page 75: Konigsberg Bridge Problem

Activity-on-Vertex (AOV) Networks

• Definition: A directed graph G in which the vertices represent tasks or activities and the edges represent precedence relations between tasks is an activity-on-vertex network or AOV network.

• Definition: Vertex i in an AOV network G is a predecessor of vertex j iff there is a directed path from vertex i to vertex j. i is an immediate predecessor of j iff <i, j> is an edge in G. If i is a predecessor of j, then j is an successor of i. If i is an immediate predecessor of j, then j is an immediate successor of i.

Page 76: Konigsberg Bridge Problem

Activity-on-Vertex (AOV) Networks (Cont.)

• Definition: A relation · is transitive iff it is the case that for all triples, i, j, k, i.j and j·k => i·k. A relation · is irreflexive on a set S if for no element x in S it is the case that x·x. A precedence relation that is both transitive and irreflexive is a partial order.

• Definition: A topological order is a linear ordering of the vertices of a graph such that, for any two vertices I and j, if I is a predecessor of j in the network, then i precedes j in the linear ordering.

Page 77: Konigsberg Bridge Problem

An Activity-on-Vertex (AOV) Network

Course number Course name PrerequisitesC1 Programming I NoneC2 Discrete Mathematics NoneC3 Data Structures C1, C2C4 Calculus I NoneC5 Calculus II C4C6 Linear Algebra C5C7 Analysis of Algorithms C3, C6C8 Assembly Language C3C9 Operating Systems C7, C8C10 Programming Languages C7C11 Compiler Design C10C12 Artificial Intelligence C7C13 Computational Theory C7C14 Parallel Algorithms C13C15 Numerical Analysis C5

Page 78: Konigsberg Bridge Problem

Topological order• The order which would not violate

the order sequences

1: count[i]=0 者 psush 到 stack 中,若有多個則用 count link 在一起2: 由 stack 中取出,將其下面的人做 update a:count 減一,若減過後成為 0 值者 push 到 stack 中3: repeat 2 直到 n 個 nodes 被處理過

Page 79: Konigsberg Bridge Problem

Program 6.12void class Graph::TopologicalOrder()// The n vectors of a network are listed in topological order{

int top=-1;for (int i=0;i<n;i++) //create a linked stack of vertices withif (count[i]==0) {count[i]=top; top=i; } //no predecessors

for (i=0;i<n;i++)if (top==-1) {cout<<“network has a cycle”<<endl; return; }else {int j=top; top=count[top]; //unstack a vertexcout<<j<<endl;ListIterator<int>li(HeadNodes[j]);if(!li.NotNull()) continue;int k=*li.First();while(1) { //decrease the count of the successor vertices of jcount[k]--;if (count[k]==0) {count[k]=top; top=k;} // add vertex k to stackif (li.NextNotNull()) k=*li.Next(); // k is successor of jelse break;}} //end of else

}

Link 到前一個

Traverse 其 list 之用

Remove 一個起點,並將此點後者之 count 做update ,若其已為 0 ,則加到stack 中

Page 80: Konigsberg Bridge Problem

An Activity-on-Vertex (AOV) Network

C1

C2

C3

C5C4 C6 C15

C7

C8

C13

C12

C10

C9

C14

C11

Topological order: c1, c2, c4, c5, c6, c8, c7, c10, c13, c12, c14, c15, c11, c9

C4, c5, c2, c1, c6, c3, c8, c15, c7, c9, c10, c11, c12, c13, c14

Page 81: Konigsberg Bridge Problem

Identify the topological order

• Include the vertex without precedence

• Delete the included vertex and its edges

• From the remaining, find the one without precedence and repeat

Page 82: Konigsberg Bridge Problem

Figure 6.36 Action of Program 6.11 on an AOV

network

0

1

2

3

4

5

1

2

3

4

5

1

2 4

5

1

4

5

1

4 4

(a) Initial (b) Vertex 0 deleted (c) Vertex 3 deleted

(d) Vertex 2 deleted (e) Vertex 5 deleted (f) Vertex 1 deleted

Page 83: Konigsberg Bridge Problem

Figure 6.37 Internal representation used by

topological sorting algorithm

14 045

2

5 04 0

[0][1][2][3][4]

01113 02 0[5

]

3 0count first data link

Page 84: Konigsberg Bridge Problem

An AOE Network1

0 4

2

6

8

7

3 5

start finish

a1 = 6 a4 = 6

a2 = 4a5 = 1

a7= 9a10 = 2

a3 = 5

a6 = 2

a9 = 4

a11 = 4

event interpretation0 Start of project1 Completion of activity a1

4 Completion of activities a4 and a5

7 Completion of activities a8 and a9

8 Completion of project

a8= 7

Page 85: Konigsberg Bridge Problem

Vertex: event

Edge: task with the number representing the time unit for finishing the task

Earliest time an event can occur:

ee(i) : earliest time an event i can occur = the longest path from source to vertex ile(i) : latest event time

e(i) : the earliest time an activity i can start

l(i) : the latest time that an activity can start without increasing project duration

Critical activity: activity which e(i)=l(i)

l(i)-e(i): measure of the criticality of an activity

Critical path: a path from start to the end which has the longest length. E.g. 0, 1, 4, 6, 8 0, 1, 4, 7, 8

Page 86: Konigsberg Bridge Problem

Adjacency lists for Figure 6.38 (a)

[0][1][2][3][4]

011132[5

]

count first vertex

2[6] 2[7] 2 0[8]

1 64 1 04 1 05 2 06 97 4 08 2 08 4 0

dur link2 4 3 5 0

7 7 0

In-degree

indegree List中

放 indegree

Page 87: Konigsberg Bridge Problem

Modification from topological order

1. count[i]=0 者 psush 到 stack 中,並運用 count link 在一起

2. 由 stack 中取出 vertex j ,將此 vertex之每一個後面者 k 運算a) 其 count[k]-- ,計算其 ee[k]=max{ ee[k],

ee[j]+p.dur} j->k distanceb) 若 count[k]=0 , push k 到 stack 中

3. repeat 2 for n-1 次 ( 或直到 stack 為空的 )( 若未達共 n 次,而 stack 以空表有cycle 存在 )

Page 88: Konigsberg Bridge Problem

Computation of ee ( earliest event time)

ee [0] [1] [2] [3] [4] [5] [6] [7] [8] StackInitial 0 0 0 0 0 0 0 0 0 [0]output 0 0 6 4 5 0 0 0 0 0 [3,2,1]output 3 0 6 4 5 0 7 0 0 0 [5,2,1]output 5 0 6 4 5 0 7 0 11 0 [2,1]output 2 0 6 4 5 0 7 0 11 0 [1]output 1 0 6 4 5 5 7 0 11 0 [4]output 4 0 6 4 5 7 7 0 14 0 [7,6]output 7 0 6 4 5 7 7 16 14 18 [6]output 6 0 6 4 5 7 7 16 14 18 [8]

output 8

Forward pass

Page 89: Konigsberg Bridge Problem

Inverse Adjacency lists for Figure 6.38 (a)

[0][1][2][3][4]

311121[5

]

count first

vertex

1[6] 1[7] 0[8]

0 60 4 00 5 01 13 24 9 04 76 2

dur link

2 1

7 4 05 4 0

out-degree

outdegree

List 中為進入 node 中之人

放outdegree

00

00

Page 90: Konigsberg Bridge Problem

Modification from topological order

1. outdegree 為 0 者, push 到stack

2. Stack 中拿出,找 list 中之node , i 重新計算其 le(i)

3. 將 count[i]-- ,若為 0 則 push 到stack

4. repeat 2

Page 91: Konigsberg Bridge Problem

Computation of le ( latestliest event time)

le [0] [1] [2] [3] [4] [5] [6] [7] [8] StackInitial 18 18 18 18 18 18 18 18 18 [8]output 8 18 18 18 18 18 18 16 14 18 [7,6]output 7 18 18 18 18 7 10 16 14 18 [5,6]output 5 18 18 18 8 7 10 16 14 18 [3,6]output 3 3 18 18 8 7 10 16 14 18 [6]output 6 3 18 18 8 7 10 16 14 18 [4]output 4 3 6 6 8 7 10 16 14 18 [2,1]output 2 2 6 6 8 7 10 16 14 18 [1]output 1 0 6 6 8 7 10 16 14 18 [0]

Backward pass