Download presentation
Presentation is loading. Please wait.
Published byEdwina Gwendoline Murphy Modified over 9 years ago
2
Introduction DATA STRUCTURE – Graph
3
electronic circuits networks (roads, flights, communications) CS16 LAX JFK LAX DFW STL HNL FTL
5
Graph A graph data structure consists of a finite (and possibly mutable) set of nodes or vertices, together with a set of ordered pairs of these nodes (or, in some cases, a set of unordered pairs). These pairs are known as edges or arcs. As in mathematics, an edge (x,y) is said to point or go from x to y. The nodes may be part of the graph structure, or may be external entities represented by integer indices or references. A graph data structure may also associate to each edge some edge value, such as a symbolic label or a numeric attribute (cost, capacity, length, etc.).
8
A graph may be either undirected or directed. An undirected edge models a "two-way“ or "duplex" connection between its endpoints, while a directed edge model is a one-way connection, and is typically drawn as an arrow. A directed edge is often called an arc. An undirected graph can have at most (N+1 / 2)edges (one for each unordered pair), while a directed graph can have at most edges (N^2 )(one per ordered pair). A multigraph can have more than one edge between the same two vertices.
16
Length of path is the number of edges connecting vertices in the sequence of the vertices in the path. This number is equal to the number of vertices in the path minus one. The length of our example for path "1", "12", "19", "21" is three. Cost of path in a weighted graph, we call the sum of the weights (costs) of the edges involved in the path. In real life the road from Sofia to Madrid, for example, is equal to the length of the road from Sofia to Paris plus the length of the road from Madrid to Paris. In our example, the length of the path "1", "12", "19" and "21" is equal to 3 + 16 + 2 = 21. Loop is a path in which the initial and the final vertex of the path match. Example of vertices forming loop are "1", "12" and "19". In the same time "1", "7" and "21" do not form a loop. Looping edge we will call an edge, which starts and ends in the same vertex. In our example the vertex "14" is looped.
22
Adjacency Matrix
26
LinkList Representation
28
Operations The basic operations provided by a graph data structure G usually include: adjacent(G, x, y): tests whether there is an edge from node x to node y. neighbors(G, x): lists all nodes y such that there is an edge from x to y. add(G, x, y): adds to G the edge from x to y, if it is not there. delete(G, x, y): removes the edge from x to y, if it is there. get_node_value(G, x): returns the value associated with the node x. set_node_value(G, x, a): sets the value associated with the node x to a. get_edge_value(G, x, y): returns the value associated to the edge (x,y). set_edge_value(G, x, y, v): sets the value associated to the edge (x,y) to v.
29
Treversing a Graph
30
Breadth First Search Breadth First Search Algorithm beginning at a start Node A. Examine the first Node A. Then examine all neighbors of A. Then examine all neighbors of neighbors of A. And So on….
31
Algorithm Initialize all node to ready state. (Status 1) Put the Starting Node A in Queue and change the status to the waiting state (Status 2) Repeat Step 4 & 5 until Queue is empty. Remove the front Node N of Queue. Process N and Change the Status of N to Processed state (Status 3). Add to rear of queue all the neighbors of N that are in steady state (Status 1) and change their status to waiting state (Status 2). Exit.
32
Depth First Search Depth First Search Algorithm beginning at a start Node A. Examine the first Node A. Then examine all neighbors of A. Then examine all neighbors of neighbors of A. And So on….
33
Algorithm Initialize all node to ready state. (Status 1) Push the Starting Node A onto Stack and change the status to the waiting state (Status 2) Repeat Step 4 & 5 until Stack is empty. Pop the Top Node N of Stack. Process N and Change the Status of N to Processed state (Status 3). Push onto Stack all the neighbors of N that are in steady state (Status 1) and change their status to waiting state (Status 2). Exit.
36
Problem: Laying Telephone Wire Central office
37
Wiring: Naïve Approach Central office Expensive!
38
Wiring: Better Approach Central office Minimize the total length of wire connecting the customers
39
ORD PIT ATL STL DEN DFW DCA 10 1 9 8 6 3 2 5 7 4
40
Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges do not have an associated direction...then a spanning tree of the graph is a connected sub- graph in which there are no cycles A connected, undirected graph Four of the spanning trees of the graph
41
A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees. oror oror oror Some Spanning Trees from Graph AGraph A Spanning Trees
42
All 16 of its Spanning TreesComplete Graph
43
Finding a spanning tree To find a spanning tree of a graph, pick an initial node and call it part of the spanning tree do a search from the initial node: each time you find a node that is not in the spanning tree, add to the spanning tree both the new node and the edge you followed to get to it An undirected graphOne possible result of a BFS starting from top One possible result of a DFS starting from top
44
Minimizing costs Suppose you want to supply a set of houses (say, in a new subdivision) with: electric power water sewage lines telephone lines To keep costs down, you could connect these houses with a spanning tree (of, for example, power lines) However, the houses are not all equal distances apart To reduce costs even further, you could connect the houses with a minimum-cost spanning tree
45
Minimum Spanning Trees The Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph. 5 7 2 1 3 4 2 1 3 Complete GraphMinimum Spanning Tree
46
Minimum-cost spanning trees Suppose you have a connected undirected graph with a weight (or cost) associated with each edge The cost of a spanning tree would be the sum of the costs of its edges A minimum-cost spanning tree is a spanning tree that has the lowest cost AB ED FC 16 19 2111 33 14 18 10 6 5 A connected, undirected graph AB ED FC 16 11 18 6 5 A minimum-cost spanning tree
47
Minimum Spanning Tree (MST) it is a tree (i.e., it is acyclic) it covers all the vertices V –contains |V| - 1 edges the total cost associated with tree edges is the minimum among all possible spanning trees not necessarily unique A minimum spanning tree is a subgraph of an undirected weighted graph G, such that
48
How Can We Generate a MST? a c e d b 2 45 9 6 4 5 5 a c e d b 2 45 9 6 4 5 5
49
Finding spanning trees There are two basic algorithms for finding minimum-cost spanning trees, and both are greedy algorithms Kruskal’s algorithm: Start with N0 nodes or edges in the spanning tree, and repeatedly add the cheapest edge that does not create a cycle Here, we consider the spanning tree to consist of edges only Prim’s algorithm: Start with any one node in the spanning tree, and repeatedly add the cheapest edge, and the node it leads to, for which the node is not already in the spanning tree. Here, we consider the spanning tree to consist of both nodes and edges
50
Kruskal’s algorithm T = empty spanning tree; E = set of edges; N = number of nodes in graph; while T has fewer than N - 1 edges { remove an edge (v, w) of lowest cost from E if adding (v, w) to T would create a cycle then discard (v, w) else add (v, w) to T } Finding an edge of lowest cost can be done just by sorting the edges
53
Prim’s algorithm T = a spanning tree containing a single node s; E = set of edges adjacent to s; while T does not contain all the nodes { remove an edge (v, w) of lowest cost from E if w is already in T then discard edge (v, w) else { add edge (v, w) and node w to T add to E the edges adjacent to w } An edge of lowest cost can be found with a priority queue Testing for a cycle is automatic Hence, Prim’s algorithm is far simpler to implement than Kruskal’s algorithm
57
Prim’s algorithm Starting from empty T, choose a vertex at random and initialize V = {1), E’ ={}
58
Prim’s algorithm Choose the vertex u not in V such that edge weight from u to a vertex in V is minimal (greedy!) V={1,3} E’= {1,3)
59
Prim’s algorithm Repeat until all vertices have been chosen Choose the vertex u not in V such that edge weight from v to a vertex in V is minimal (greedy!) V= {1,3,4} E’= {(1,3),(3,4)} V={1,3,4,5} E’={(1,3),(3,4),(4,5)} …. V={1,3,4,5,2,6} E’={(1,3),(3,4),(4,5),(5,2),(2,6)}
60
Prim’s algorithm Repeat until all vertices have been chosen V={1,3,4,5,2,6} E’={(1,3),(3,4),(4,5),(5,2),(2,6 )} Final Cost: 1 + 3 + 4 + 1 + 1 = 10
61
Prim's Algorithm This algorithm starts with one node. It then, one by one, adds a node, that is unconnected to the new graph to the new graph, each time selecting the node whose connecting edge has the smallest weight out of the available nodes’ connecting edges.
62
The steps are: 1. The new graph is constructed - with one node from the old graph. 2. While new graph has fewer than n nodes, 1. Find the node from the old graph with the smallest connecting edge to the new graph, 2. Add it to the new graph Every step will have joined one node, so that at the end we will have one graph with all the nodes and it will be a minimum spanning tree of the original graph.
63
4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Complete Graph
64
4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
65
4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
66
4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
67
4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
68
4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
69
4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
70
4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
71
4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
72
4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
73
4 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J Old GraphNew Graph 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
74
4 1 2 2 1 3 3 2 4 A BC D E F G H I J Complete GraphMinimum Spanning Tree 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J
75
Prim’s Algorithm Initialization a. Pick a vertex r to be the root b. Set D(r) = 0, parent(r) = null c. For all vertices v V, v r, set D(v) = d. Insert all vertices into priority queue P, using distances as the keys a c e d b 2 45 9 6 4 5 5 eabcd 0 VertexParent e -
76
Prim’s Algorithm While P is not empty: 1. Select the next vertex u to add to the tree u = P.deleteMin() 2. Update the weight of each vertex w adjacent to u which is not in the tree (i.e., w P ) If weight(u,w) < D(w), a. parent(w) = u b. D(w) = weight(u,w) c. Update the priority queue to reflect new distance for w
77
Prim’s algorithm a c e d b 2 45 9 6 4 5 5 dbca 455 VertexParent e- be ce de The MST initially consists of the vertex e, and we update the distances and parent for its adjacent vertices VertexParent e- b- c- d- dbca e 0
78
Prim’s algorithm a c e d b 2 45 9 6 4 5 5 acb 245 VertexParent e- be c d de a d dbca 455 VertexParent e- be ce de
79
Prim’s algorithm a c e d b 2 45 9 6 4 5 5 cb 45 VertexParent e- be c d de a d acb 245 VertexParent e- be c d de a d
80
Prim’s algorithm a c e d b 2 45 9 6 4 5 5 b 5 VertexParent e- be c d de a d cb 45 VertexParent e- be c d de a d
81
Prim’s algorithm VertexParent e- be c d de a d a c e d b 2 45 9 6 4 5 5 The final minimum spanning tree b 5 VertexParent e- be c d de a d
82
Example B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 8 B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 5 7 B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 5 7 B D C A F E 7 4 2 8 5 7 3 9 8 0 7 2 5 4 7
83
Example (contd.) B D C A F E 7 4 2 8 5 7 3 9 8 0 3 2 5 4 7 B D C A F E 7 4 2 8 5 7 3 9 8 0 3 2 5 4 7
84
Another Approach a c e d b 2 45 9 6 4 5 5 Create a forest of trees from the vertices Repeatedly merge trees by adding “safe edges” until only one tree remains A “safe edge” is an edge of minimum weight which does not create a cycle forest: {a}, {b}, {c}, {d}, {e}
85
Example
86
Initialization Initially, Forest of 6 trees F= {{1},{2},{3},{4},{5},{6}} Edges in a heap (not shown)
87
Step 1 Select edge with lowest cost (2,5) Find(2) = 2, Find (5) = 5 Union(2,5) F= {{1},{2,5},{3},{4},{6}} 1 edge accepted 1
88
Step 2 Select edge with lowest cost (2,6) Find(2) = 2, Find (6) = 6 Union(2,6) F= {{1},{2,5,6},{3},{4}} 2 edges accepted 1 1
89
Step 3 Select edge with lowest cost (1,3) Find(1) = 1, Find (3) = 3 Union(1,3) F= {{1,3},{2,5,6},{4}} 3 edges accepted 1 1 1
90
Step 4 Select edge with lowest cost (5,6) Find(5) = 2, Find (6) = 2 Do nothing F= {{1,3},{2,5,6},{4}} 3 edges accepted 1 1 1
91
Step 5 Select edge with lowest cost (3,4) Find(3) = 1, Find (4) = 4 Union(1,4) F= {{1,3,4},{2,5,6}} 4 edges accepted 1 1 1 3
92
Step 6 Select edge with lowest cost (4,5) Find(4) = 1, Find (5) = 2 Union(1,2) F= {{1,3,4,2,5,6}} 5 edges accepted : end Total cost = 10 Although there is a unique spanning tree in this example, this is not generally the case 1 1 1 3 4
93
Kruskal's Algorithm This algorithm creates a forest of trees. Initially the forest consists of n single node trees (and no edges). At each step, we add one edge (the cheapest one) so that it joins two trees together. If it were to form a cycle, it would simply link two nodes that were already part of a single connected tree, so that this edge would not be needed.
94
The steps are: 1. The forest is constructed - with each node in a separate tree. 2. The edges are placed in a priority queue. 3. Until we've added n-1 edges, 1. Extract the cheapest edge from the queue, 2. If it forms a cycle, reject it, 3. Else add it to the forest. Adding it to the forest will join two trees together. Every step will have joined two trees in the forest together, so that at the end, there will only be one tree in T.
95
4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Complete Graph
96
1 4 2 5 2 5 4 3 4 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J AABD BB B CD JC C E F D DH JEG FFGI GGIJ HJJI
97
2 5 2 5 4 3 4 4 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Sort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize)
98
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge
99
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge
100
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge
101
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge
102
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge
103
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Cycle Don’t Add Edge
104
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge
105
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge
106
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge
107
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Cycle Don’t Add Edge
108
2 5 2 5 4 3 4 4 10 1 6 3 3 2 1 2 3 2 1 3 5 3 4 2 56 4 4 A BC D E F G H I J B B D J C C E F D DH J EG F F G I G G I J HJ JI 1 AD 4 BC 4 AB Add Edge
109
4 1 2 2 1 3 3 2 4 A BC D E F G H I J 4 1 2 3 2 1 3 5 3 4 2 56 4 4 10 A BC D E F G H I J Minimum Spanning TreeComplete Graph
110
Example
111
Initialization Initially, Forest of 6 trees F= {{1},{2},{3},{4},{5},{6}} Edges in a heap (not shown)
112
Step 1 Select edge with lowest cost (2,5) Find(2) = 2, Find (5) = 5 Union(2,5) F= {{1},{2,5},{3},{4},{6}} 1 edge accepted 1
113
Step 2 Select edge with lowest cost (2,6) Find(2) = 2, Find (6) = 6 Union(2,6) F= {{1},{2,5,6},{3},{4}} 2 edges accepted 1 1
114
Step 3 Select edge with lowest cost (1,3) Find(1) = 1, Find (3) = 3 Union(1,3) F= {{1,3},{2,5,6},{4}} 3 edges accepted 1 1 1
115
Step 4 Select edge with lowest cost (5,6) Find(5) = 2, Find (6) = 2 Do nothing F= {{1,3},{2,5,6},{4}} 3 edges accepted 1 1 1
116
Step 5 Select edge with lowest cost (3,4) Find(3) = 1, Find (4) = 4 Union(1,4) F= {{1,3,4},{2,5,6}} 4 edges accepted 1 1 1 3
117
Step 6 Select edge with lowest cost (4,5) Find(4) = 1, Find (5) = 2 Union(1,2) F= {{1,3,4,2,5,6}} 5 edges accepted : end Total cost = 10 Although there is a unique spanning tree in this example, this is not generally the case 1 1 1 3 4
118
Kruskal’s algorithm Initialization a. Create a set for each vertex v V b. Initialize the set of “safe edges” A comprising the MST to the empty set c. Sort edges by increasing weight a c e d b 2 45 9 6 4 5 5 F = {a}, {b}, {c}, {d}, {e} A = E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)}
119
Kruskal’s algorithm For each edge (u,v) E in increasing order while more than one set remains: If u and v, belong to different sets U and V a. add edge (u,v) to the safe edge set A = A {(u,v)} b. merge the sets U and V F = F - U - V + (U V) Return A
120
Kruskal’s algorithm E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)} Forest {a}, {b}, {c}, {d}, {e} {a,d}, {b}, {c}, {e} {a,d,c}, {b}, {e} {a,d,c,e}, {b} {a,d,c,e,b} A {(a,d)} {(a,d), (c,d)} {(a,d), (c,d), (d,e)} {(a,d), (c,d), (d,e), (b,e)} a c e d b 2 45 9 6 4 5 5
121
Kruskal Example JFK BOS MIA ORD LAX DFW SFO BWI PVD 867 2704 187 1258 849 144 740 1391 184 946 1090 1121 2342 1846 621 802 1464 1235 337
125
Example Minimum Spanning Trees125
127
Example Minimum Spanning Trees
128
Example Minimum Spanning Trees
129
Example Minimum Spanning Trees
130
Example Minimum Spanning Trees
131
Example Minimum Spanning Trees
132
Example Minimum Spanning Trees
134
144 740 1391 184 946 1090 1121 2342 1846 621 802 1464 1235 337
135
Shortest Path Problem Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v. Length of a path is the sum of the weights of its edges. Example: Shortest path between X & Y Applications Internet packet routing Flight reservations Driving directions Shortest Paths ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 1205
136
In an edge-weighted graph, the weight of an edge measures the cost of traveling that edge. For example, in a graph representing a network of airports, the weights could represent: distance, cost or time. Such a graph could be used to answer any of the following: What is the fastest way to get from A to B? Which route from A to B is the least expensive? What is the shortest possible distance from A to B? Each of these questions is an instance of the same problem: The shortest path problem!
137
Variants of Shortest Path Single-source shortest paths G = (V, E) find a shortest path from a given source vertex s to each vertex v V Single-destination shortest paths Find a shortest path to a given destination vertex t from each vertex v Reversing the direction of each edge single-source
138
Variants of Shortest Paths (cont’d) Single-pair shortest path Find a shortest path from u to v for given vertices u and v All-pairs shortest-paths Find a shortest path from u to v for every pair of vertices u and v
139
Example CB A E D F 0 428 4 8 71 25 2 39 C B A E D F 0 328 511 4 8 71 25 2 39 C B A E D F 0 328 58 4 8 71 25 2 39 C B A E D F 0 327 58 4 8 71 25 2 39
140
Example (cont.) Shortest Paths CB A E D F 0 327 58 4 8 71 25 2 39 CB A E D F 0 327 58 4 8 71 25 2 39
143
Dijkstra’s Algorithm A priority queue stores the vertices outside the cloud Key: distance Element: vertex Locator-based methods insert(k,e) returns a locator replaceKey(l,k) changes the key of an item We store two labels with each vertex: Distance (d(v) label) locator in priority queue Algorithm DijkstraDistances(G, s) Q new heap-based priority queue for all v G.vertices() if v s setDistance(v, 0) else setDistance(v, ) l Q.insert(getDistance(v), v) setLocator(v,l) while Q.isEmpty() u Q.removeMin() for all e G.incidentEdges(u) { relax edge e } z G.opposite(u,e) r getDistance(u) weight(e) if r getDistance(z) setDistance(z,r) Q.replaceKey(getLocator(z),r)
144
Dijkstra Algorithm (Single Source Shortest Path Problem Algorithm) Dijkstra's algorithm solves the problem of finding the shortest path from a point in a graph (the source) to a destination. It turns out that one can find the shortest paths from a given source to all points in a graph in the same time, hence this problem is sometimes called the single- source shortest paths problem.
145
Dijkstra Algorithm
151
g 0a 3a 5a 5b 11c 8c 9f E 0a 3a 5a 5b 11c 8c 9f
152
Warshall’s Algorithm Suppose G is a directed graph with n nodes v 1, v 2,..., v n and we want to find the path matrix of graph G. For this purpose, Warshall gave an algorithm to find the transitive closure of graph. The n-square boolean matrix, P0, P 1,..., Pn is defined as follows. Suppose Pk [i] [j] denote the ij entry of the matrix Pk. So
153
Warshall’s Algorithm (Contd.) In other words, P 0 [i] [j] = 1 If there is an edge between vi to vj P 1 [i] [j] = 1 If there is a path from vi to vj which does not use any other nodes except possible v1 P2 [i] [j] = 1 If there is a path from vi to vj which does not use any other nodes except possibly v1 and v2
154
Warshall’s Algorithm (Contd.) Warshall stated that Pk [i] [j] = 1 can occur only if one of the following two cases occurs: (a) There is a path from vi to vj which does not use any other nodes except possibly v1, v2,... vk–1, hence P k–1 [i] [j] = 1 (b) There is a path form vi to vk and a path from vk to vj where each path does not use any other nodes except possibly v1, v2,..., vk–1, hence P k–1 [i] [k] = 1 and P k–1 [k] [j] = 1 vi …. vj vi …. vk …. vj
155
Warshall’s Algorithm (Contd.) So the elements of matrix Pk can be obtained by P k [i] [j] = P k–1 [i] [j] V (P k–1 [i] [k] P k–1 [k] [j])
156
Warshall’s Algorithm (Contd.) 1. [Initialize P] Repeat for i, j = 1, 2,..., n If A[i] [j] = 0 then set P [i] [j] = 0 else set P[i] [j] = 1 2. Repeat steps 3 to 4 for k = 1, 2,..., n 3. Repeat step 4 for i = 1, 2,..., n 4. Repeat for j = 1, 2,..., n Set P [i] [j] = P [i] [j] V (P [i] [k] P [k] [j]) 5. Exit
160
Activity Networks Activity on Network To simplify a project, divide it into several subprojects called activities. The successful completion of these activities will result in a completion of entire project for e.g., A student can take DS if he have read programming previously but some other paper, he can take which does not depend upon it. This relationship can be represented by a directed graph in which the vertices represent courses and the directed edges represents prerequisitces. That is, a directed graph G in which the vertices represent tasks or activities and the edges represent precedence relations between tasks is an activity on network.
161
Topological Sorting
181
Transitive Closure
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.