184
WELCOME TO THETOPPERSWAY.COM

Introduction DATA STRUCTURE – Graph electronic circuits networks (roads, flights, communications) CS16 LAX JFK LAX DFW STL HNL FTL

Embed Size (px)

Citation preview

Unit 1

WELCOME TOTHETOPPERSWAY.COMUnit 4IntroductionDATA STRUCTURE Graphelectronic circuits

networks (roads, flights, communications)

CS16LAXJFKLAXDFWSTLHNLFTL

GraphA 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.).

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.

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.

Adjacency Matrix

LinkList Representation

Node ListEdge ListLinkList Representation

OperationsThe 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.Treversing a GraphDuring the execution of Algorithms, each node N of G will be in one of the three status:Status 1: (Ready State) the initial state of Node NStatus 2: (Waiting State) The Node N is on the Queue or Stack waiting to processedStatus 3: (Processed State) The Node N has been Processed.

Breadth First SearchBreadth 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.

AlgorithmInitialize 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.Depth First SearchDepth 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.

AlgorithmInitialize 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.

Problem: Laying Telephone WireCentral officeWiring: Nave ApproachCentral officeExpensive!Wiring: Better ApproachCentral officeMinimize the total length of wire connecting the customersORDPITATLSTLDENDFWDCA10198632574Spanning treesSuppose you have a connected undirected graphConnected: every node is reachable from every other nodeUndirected: edges do not have an associated direction...then a spanning tree of the graph is a connected sub-graph in which there are no cyclesA connected,undirected graphFour of the spanning trees of the graph40A 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.orororSome Spanning Trees from Graph AGraph ASpanning TreesAll 16 of its Spanning TreesComplete GraphFinding a spanning treeTo find a spanning tree of a graph,pick an initial node and call it part of the spanning treedo 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 itAn undirected graphOne possible result of a BFSstarting from topOne possible result of a DFSstarting from top43Minimizing costsSuppose you want to supply a set of houses (say, in a new subdivision) with:electric powerwatersewage linestelephone linesTo 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 apartTo reduce costs even further, you could connect the houses with a minimum-cost spanning tree44Minimum Spanning TreesThe Minimum Spanning Tree for a given graph is the Spanning Tree of minimum cost for that graph.572134213Complete GraphMinimum Spanning TreeMinimum-cost spanning treesSuppose you have a connected undirected graph with a weight (or cost) associated with each edgeThe cost of a spanning tree would be the sum of the costs of its edgesA minimum-cost spanning tree is a spanning tree that has the lowest costABEDFC161921113314181065A connected, undirected graphABEDFC16111865A minimum-cost spanning tree46Minimum Spanning Tree (MST)it is a tree (i.e., it is acyclic)it covers all the vertices Vcontains |V| - 1 edgesthe total cost associated with tree edges is the minimum among all possible spanning treesnot necessarily uniqueA minimum spanning tree is a subgraph of an undirected weighted graph G, such thatHow Can We Generate a MST? acedb24596455acedb24596455Finding spanning treesThere are two basic algorithms for finding minimum-cost spanning trees, and both are greedy algorithms

Kruskals algorithm: Start with N0 nodes or edges in the spanning tree, and repeatedly add the cheapest edge that does not create a cycleHere, we consider the spanning tree to consist of edges only

Prims 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 edges49Kruskals algorithmT = 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 cyclethen discard (v, w)else add (v, w) to T}

Finding an edge of lowest cost can be done just by sorting the edges50

Prims algorithmT = 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 Eif w is already in T then discard edge (v, w)else {add edge (v, w) and node w to Tadd to E the edges adjacent to w}}An edge of lowest cost can be found with a priority queue Testing for a cycle is automaticHence, Prims algorithm is far simpler to implement than Kruskals algorithm53

Prims algorithm

Starting from empty T, choose a vertex at random and initializeV = {1), E ={}Prims 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)Prims algorithm

Repeat until all vertices have been chosenChoose 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)}Prims algorithm

Repeat until all vertices have been chosenV={1,3,4,5,2,6}E={(1,3),(3,4),(4,5),(5,2),(2,6)}

Final Cost: 1 + 3 + 4 + 1 + 1 = 10Prim's AlgorithmThis 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.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.41232135342564410ABCDEFGHIJComplete Graph41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ41232135342564410ABCDEFGHIJOld GraphNew Graph41232135342564410ABCDEFGHIJ412213324ABCDEFGHIJComplete GraphMinimum Spanning Tree41232135342564410ABCDEFGHIJPrims AlgorithmInitializationa. Pick a vertex r to be the root b. Set D(r) = 0, parent(r) = nullc. For all vertices v V, v r, set D(v) = d. Insert all vertices into priority queue P, using distances as the keysacedb24596455eabcd0VertexParent e -Prims AlgorithmWhile P is not empty:

1. Select the next vertex u to add to the treeu = 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 wPrims algorithmacedb24596455dbca455VertexParente-becedeThe MST initially consists of the vertex e, and we updatethe distances and parent for its adjacent verticesVertexParente-b-c-d-dbcae0Prims algorithmacedb24596455acb245VertexParente-bec ddea ddbca455VertexParente-becedePrims algorithmacedb24596455cb45VertexParente-bec ddea dacb245VertexParente-bec ddea dPrims algorithmacedb24596455b5VertexParente-bec ddea dcb45VertexParente-bec ddea dPrims algorithmVertexParente-bec ddea dacedb24596455The final minimum spanning treeb5VertexParente-bec ddea dExampleBDCAFE7428573980728BDCAFE74285739807257BDCAFE74285739807257BDCAFE742857398072547Example (contd.)BDCAFE742857398032547BDCAFE742857398032547Another Approachacedb24596455Create a forest of trees from the verticesRepeatedly merge trees by adding safe edges until only one tree remainsA safe edge is an edge of minimum weight which does not create a cycleforest: {a}, {b}, {c}, {d}, {e}Example

Initialization

Initially, Forest of 6 treesF= {{1},{2},{3},{4},{5},{6}}

Edges in a heap (not shown)

Step 1

Select edge with lowest cost (2,5)Find(2) = 2, Find (5) = 5Union(2,5)F= {{1},{2,5},{3},{4},{6}}1 edge accepted

1Step 2

Select edge with lowest cost (2,6)Find(2) = 2, Find (6) = 6Union(2,6)F= {{1},{2,5,6},{3},{4}}2 edges accepted

11Step 3

Select edge with lowest cost (1,3)Find(1) = 1, Find (3) = 3Union(1,3)F= {{1,3},{2,5,6},{4}}3 edges accepted

111Step 4

Select edge with lowest cost (5,6)Find(5) = 2, Find (6) = 2Do nothingF= {{1,3},{2,5,6},{4}}3 edges accepted

111Step 5

Select edge with lowest cost (3,4)Find(3) = 1, Find (4) = 4Union(1,4)F= {{1,3,4},{2,5,6}}4 edges accepted

1113Step 6

Select edge with lowest cost (4,5)Find(4) = 1, Find (5) = 2Union(1,2)F= {{1,3,4,2,5,6}}5 edges accepted : endTotal cost = 10Although there is a unique spanning tree in this example, this is not generally the case

11134Kruskal's AlgorithmThis 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.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.41232135342564410ABCDEFGHIJComplete Graph1425254344410163321232135342564410ABCDEFGHIJAABDBBBCDJCCEFDDHJEGFFGIGGIJHJJI2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABSort Edges (in reality they are placed in a priority queue - not sorted - but sorting them makes the algorithm easier to visualize)2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABAdd Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABAdd Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABAdd Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABAdd Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABAdd Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABCycleDont Add Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABAdd Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABAdd Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABAdd Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABCycleDont Add Edge2525434410163321232135342564410ABCDEFGHIJBBDJCCEFDDHJEGFFGIGGIJHJJI1AD4BC4ABAdd Edge412213324ABCDEFGHIJ41232135342564410ABCDEFGHIJMinimum Spanning TreeComplete GraphExample

Initialization

Initially, Forest of 6 treesF= {{1},{2},{3},{4},{5},{6}}

Edges in a heap (not shown)

Step 1

Select edge with lowest cost (2,5)Find(2) = 2, Find (5) = 5Union(2,5)F= {{1},{2,5},{3},{4},{6}}1 edge accepted

1Step 2

Select edge with lowest cost (2,6)Find(2) = 2, Find (6) = 6Union(2,6)F= {{1},{2,5,6},{3},{4}}2 edges accepted

11Step 3

Select edge with lowest cost (1,3)Find(1) = 1, Find (3) = 3Union(1,3)F= {{1,3},{2,5,6},{4}}3 edges accepted

111Step 4

Select edge with lowest cost (5,6)Find(5) = 2, Find (6) = 2Do nothingF= {{1,3},{2,5,6},{4}}3 edges accepted

111Step 5

Select edge with lowest cost (3,4)Find(3) = 1, Find (4) = 4Union(1,4)F= {{1,3,4},{2,5,6}}4 edges accepted

1113Step 6

Select edge with lowest cost (4,5)Find(4) = 1, Find (5) = 2Union(1,2)F= {{1,3,4,2,5,6}}5 edges accepted : endTotal cost = 10Although there is a unique spanning tree in this example, this is not generally the case

11134Kruskals algorithmInitializationa. Create a set for each vertex v Vb. Initialize the set of safe edges A comprising the MST to the empty setc. Sort edges by increasing weightacedb24596455F = {a}, {b}, {c}, {d}, {e}A = E = {(a,d), (c,d), (d,e), (a,c), (b,e), (c,e), (b,d), (a,b)}Kruskals algorithmFor 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 AKruskals algorithmE = {(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)}acedb24596455Kruskal ExampleJFKBOSMIAORDLAXDFWSFOBWIPVD867270418712588491447401391184946109011212342184662180214641235337

ExampleMinimum Spanning Trees125

ExampleMinimum Spanning Trees

ExampleMinimum Spanning Trees

ExampleMinimum Spanning Trees

ExampleMinimum Spanning Trees

ExampleMinimum Spanning Trees

ExampleMinimum Spanning Trees

Minimum Spanning Trees

JFKBOSMIAORDLAXDFWSFOBWIPVD867270418712588491447401391184946109011212342184662180214641235337Shortest Path ProblemGiven 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 & YApplicationsInternet packet routing Flight reservationsDriving directionsShortest PathsORDPVDMIADFWSFOLAXLGAHNL84980213871743184310991120123333725551421205In 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!

Variants of Shortest PathSingle-source shortest pathsG = (V, E) find a shortest path from a given source vertex s to each vertex v VSingle-destination shortest pathsFind a shortest path to a given destination vertex t from each vertex vReversing the direction of each edge single-source137Variants of Shortest Paths (contd)Single-pair shortest pathFind a shortest path from u to v for given vertices u and vAll-pairs shortest-pathsFind a shortest path from u to v for every pair of vertices u and v138ExampleCBAEDF0428487125239CBAEDF0328511487125239CBAEDF032858487125239CBAEDF032758487125239Example (cont.)Shortest PathsCBAEDF032758487125239CBAEDF032758487125239

Dijkstras AlgorithmA priority queue stores the vertices outside the cloudKey: distanceElement: vertexLocator-based methodsinsert(k,e) returns a locator replaceKey(l,k) changes the key of an itemWe store two labels with each vertex:Distance (d(v) label)locator in priority queueAlgorithm DijkstraDistances(G, s)Q new heap-based priority queuefor all v G.vertices()if v = ssetDistance(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)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 (thesource) to a destination. It turns out that one can find the shortest paths from a given source toallpoints in a graph in the same time, hence this problem is sometimes called thesingle-source shortest pathsproblem.

Dijkstra Algorithm

g 0a 3a 5a 5b 11c 8c 9fE 0a 3a 5a 5b 11c 8c 9fWarshalls AlgorithmSuppose G is a directed graph with n nodes v1, v2, ..., vn 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, P1,..., Pn is defined as follows. Suppose Pk [i] [j] denote the ij entry of the matrix Pk. So

Warshalls Algorithm (Contd.)In other words,P0 [i] [j] = 1 If there is an edge between vi to vjP1 [i] [j] = 1 If there is a path from vi to vj which does not use any other nodes except possible v1P2 [i] [j] = 1 If there is a path from vi to vj which does not use any other nodes except possibly v1 and v2

Warshalls 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,... vk1, hencePk1 [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, ..., vk1, hencePk1 [i] [k] = 1 and Pk1 [k] [j] = 1vi . vjvi . vk . vj

Warshalls Algorithm (Contd.)So the elements of matrix Pk can be obtained by

Pk [i] [j] = Pk1 [i] [j] V (Pk1 [i] [k] Pk1 [k] [j])

Warshalls Algorithm (Contd.)1. [Initialize P]Repeat for i, j = 1, 2, ..., nIf A[i] [j] = 0 then set P [i] [j] = 0else set P[i] [j] = 12. Repeat steps 3 to 4 for k = 1 , 2, ..., n3. Repeat step 4 for i = 1, 2, ..., n4. Repeat for j = 1, 2, ..., nSet P [i] [j] = P [i] [j] V (P [i] [k] P [k] [j])5. Exit

Activity NetworksActivity on NetworkTo 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.

Topological Sorting

Transitive Closure

THANK YOUFOR VISITINGTHETOPPERSWAY.COM