Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path.

Similar presentations


Presentation on theme: "Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path."— Presentation transcript:

1 Graphs Part II Lecture 7

2 Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path

3 Applications of Graph There are many useful applications of graphs. There are many useful applications of graphs. Some common applications are as follows Some common applications are as follows Topological Sort Spanning Tree Minimum Spanning Tree Shortest Path Traveling Salesman Problem Three Utilities Problem Graph Coloring Problem

4 Topological Sort A directed graph without cycles has a natural order. A directed graph without cycles has a natural order. Example : Vertex a precedes vertex b which precedes vertex c. Example : Vertex a precedes vertex b which precedes vertex c. There is a linear order, called topological order of the vertices in the directed graph without cycles In the list of vertices in topological order vertex x precedes vertex y if there is a directed edge from x to y in the graph If the graph represent academic courses then the graph represents the prerequisite structure for the courses. Course a is the prerequisite for course b which is the prerequisite for c and e. Graph

5 The vertex in the graph may have several topological orders. Example : a,g,d,b,e,c,f and a,b,g,d,e,f,c If you arrange the vertices of a directed graph linearly and in topological order, the edges will point in one direction Topological Sort Graph Arranging the vertices into a topological order is called topological sorting.

6 Simple algorithm for top sort1 Working : 1. Find a vertex which has no successor. 2. Remove it from the graph and all edges that lead to it. 3. Add it to the beginning of the list of vertices 4. Do the same for the remaining vertices in the graph. 5. Result : When the graph is empty the list of vertices will be in topological order.

7 1. N = number of vertices in the graph 2. For ( step = 1 through n) { select a vertex v that has no successors aList.insert (1,v) Delete from the graph vertex v and its edges } When the traversal ends the list of vertices will be in topological order. When the traversal ends the list of vertices will be in topological order. Simple algorithm for top sort1

8

9 Spanning Tree A Tree is a special kind of graph with no cycles. A Tree is a special kind of graph with no cycles. All trees are graphs but not all graphs are trees All trees are graphs but not all graphs are trees The nodes of a tree have a hierarchical arrangement that is not required of all graphs The nodes of a tree have a hierarchical arrangement that is not required of all graphs A spanning tree of a connected undirected graph G is a sub graph of G that contains all vertices of G and enough edges to form a tree. A spanning tree of a connected undirected graph G is a sub graph of G that contains all vertices of G and enough edges to form a tree.Graph Spanning Tree

10 There may be several spanning tree for a given graph Working : If you have a connected undirected graph with cycles and you remove edges until there are no cycles, then you will obtain a spanning tree for the graph Observation about an undirected graph to detect a cycle A connected undirected graph that has n vertices must have at least n-1 edges A connected undirected graph that has n vertices and exactly n-1 edges cannot contain a cycle A connected undirected graph that has n vertices and more than n-1 edges must contain at least one cycle

11 Algorithms Two algorithms are used for determining a spanning tree of graph. Two algorithms are used for determining a spanning tree of graph. These algorithms produce different spanning trees for any particular graph. These algorithms produce different spanning trees for any particular graph. 1. DFS Spanning Tree 2. BFS Spanning Tree One way to determine the spanning tree is to traverse the graph's vertices by using a Depth first search. After the traversal the graph’s vertices and the marked edges form the spanning tree called DFS spanning tree Another way to determine the spanning tree is to traverse the graph's vertices by using a Breadth first search. After the traversal the graph’s vertices and the marked edges form the spanning tree called BFS spanning tree

12 DFS Spanning Tree Dfstree(in v:vertex) Mark v as visited for ( each unvisited vertex u adjacent to v ) { Mark the edge from u to v Dfstree(u)} After DFS

13 BFS Spanning Tree Bfstree(in v:vertex) q.createQueue()q.enqueue(v) Mark v as visited while (!q.empty()) { q.dequeue(w) for ( each unvisited vertex u adjacent to w) for ( each unvisited vertex u adjacent to w) { Mark u as visited Mark edge between w and u Mark edge between w and u q.enqueue(u) q.enqueue(u)}} After BFS

14 Minimum Spanning Trees Objective : To minimize the length of a connecting network Objective : To minimize the length of a connecting network The resulting connection graph is connected, undirected, and acyclic, i.e., a free tree (sometimes called simply a tree). The resulting connection graph is connected, undirected, and acyclic, i.e., a free tree (sometimes called simply a tree). So a minimum spanning tree (MST) is a spanning tree of minimum weight (cost).

15 Example Graph Cost of the spanning tree is the sum of the cost of the edges in the spanning tree Selecting the least cost spanning tree is minimum cost spanning tree

16 Algorithms Two algorithms for computing MSTs: Two algorithms for computing MSTs: –Kruskal’s Algorithm –Prim’s Algorithm Both are variants of the same generic approach.Both are variants of the same generic approach.

17 Kruskal’s Algorithm Working : 1.If the next edge does not induce a cycle among the current set of edges, then it is added to A. 2.If it does, then this edge is passed over, and we consider the next edge in order. 3.As this algorithm runs, the edges of A will induce a forest on the vertices and the trees of this forest are merged together until we have a single tree containing all vertices. Kruskal attempts to add edges to A in increasing order of weight (lightest edge first). Image next slide

18 Kruskal’s Algorithm

19 Prim’s Algorithm Prim works by adding leaves one at a time to the current tree. Prim works by adding leaves one at a time to the current tree. Working : 1.Start with the root vertex r (it can be any vertex). 2.At each step select, a light edge connecting a vertex in tree to a vertex not in tree 3.The tree grows until it spans all the vertices

20 Primsalgorithm(in v:vertex) Mark vertex v as visited and include it in the minimum spanning tree while ( there are unvisited vertices) { find the least cost edge (v,u) from a visited vertex v to some unvisited vertex u Mark u as visited Add the vertex u and the edge (v,u) to the minimum spanning tree } Prim’s Algorithm Trace of this algorithm - Next Slide

21 Prim’s Algorithm GraphSteps Not yet complete... Leave it as exercise

22 Shortest Path Problem We are given a graph G = (V, E) with weight function w We are given a graph G = (V, E) with weight function w The weight of a path p = is the sum of edge weights along the path. The weight of a path p = is the sum of edge weights along the path. The shortest-path weight between two vertices, u and v, is the length of the minimum weight path from u to v The shortest-path weight between two vertices, u and v, is the length of the minimum weight path from u to v Given a directed graph G = (V, E) with edge weights and a distinguished source vertex, determine the distance from the source vertex to every vertex in the graph. Single-Source Shortest Paths

23 Edge weights can be negative; but in order for the problem to be well-defined there must be no cycle whose total cost is negative. Why? Edge weights can be negative; but in order for the problem to be well-defined there must be no cycle whose total cost is negative. Why? In general, we assume there are no cycles at all: In general, we assume there are no cycles at all: –Zero-weight cycles clearly can be removed as they do not affect total path weights. –Negative cycles must be removed. –Removing positive-weight cycles from a path will reduce the weight of the path, so they also should be removed.

24 Variants Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from every vertex. Single-destination shortest-paths problem: Find a shortest path to a given destination vertex t from every vertex. Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. If we solve the single-source shortest paths problem, we also solve this problem. Single-pair shortest-path problem: Find a shortest path from u to v for given vertices u and v. If we solve the single-source shortest paths problem, we also solve this problem. All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v. This can be solved by the single-source problem run for each vertex. All-pairs shortest-paths problem: Find a shortest path from u to v for every pair of vertices u and v. This can be solved by the single-source problem run for each vertex.

25 SSP – Dijkstra’s Algorithm Working: 1. It determines the shortest path from given origin and all other vertices. 2. The algorithm uses a set of vertex set of selected vertices and an array weight where weight of the shortest path from vertex 0 to vertex v that passes through vertices in the vertex set. 3. If v is in vertex set the shortest path involves only vertices in vertex set. However if v is not in vertex set then v is the only vertex along the path that is not in vertex set. That is the path ends with an edge from a vertex in vertex set to v. 4. Initially the vertex set contains only vertex 0, and weight contains the weights of the single edge paths from vertex 0 to all other vertices. That is weight[v] equals matrix[0][v] for all v, where matrix is the adjacency matrix. Thus initially the weight is the first row of the matrix.

26 SSP – Dijkstra’s Algorithm Cont... After initialization find a vertex v that is not in vertex set and that minimizes weight[v]. Add v to vertex set. For all unselected vertices u are not in vertex set check the values weight[u] to ensure that they are indeed minimum. After initialization find a vertex v that is not in vertex set and that minimizes weight[v]. Add v to vertex set. For all unselected vertices u are not in vertex set check the values weight[u] to ensure that they are indeed minimum. To make this determination break the path from 0 to u into two pieces and find their weights as follows: To make this determination break the path from 0 to u into two pieces and find their weights as follows: Weight[v] = weight of the shortest path from 0 to v Matrix[v][u] = weight of the shortest edge from v to u Compare the weight[u] with weight[v] |+ matrix[v][u] and let Compare the weight[u] with weight[v] |+ matrix[v][u] and let Weight [u] = the smaller of the values weight[u] and weight[v] + matrix [v] [u] Weight [u] = the smaller of the values weight[u] and weight[v] + matrix [v] [u]

27 Example: Dijkstra’s Algorithm 0    2 7 32 8 5 1 54 e c b d a

28 0    2 7 32 8 5 1 54 e c b d a

29 0   7  2 7 32 8 5 1 54 e c b d a

30 0 2  7  2 7 32 8 5 1 54

31 0 2  7  2 7 32 8 5 1 54

32 0 2 105 7 2 7 32 8 5 1 54

33 Example: Dijkstra’s Algorithm 0 2 65 7 2 7 32 8 5 1 54

34 0 2 65 7 2 7 32 8 5 1 54

35 0 2 65 7 2 7 32 8 5 1 54

36 Pseudocode 1. n = number of vertices in the graph 2. For(v=0 through n-1) weight [v] = matrix[0][v] 3.For(step =2 through n) { find the smallest weight [v] such that v is not in vertexset Add v to vertexset //check the weight[u] for all u not in vertexset For(all vertices u not in vertexset ) if (weight [u] > weight [v] + matrix [v][u]) if (weight [u] > weight [v] + matrix [v][u]) weight [u] = weight [v] + matrix [v][u] weight [u] = weight [v] + matrix [v][u]}

37 Graph and the SP A trace of the shortest- path algorithm Checking weight[u] by examining the graph:

38 The Bellman-Ford Algorithm What happen if we have negative weight edges ? What happen if we have negative weight edges ? Not a problem if UNLESS there is a negative weight cycle. Why ? Not a problem if UNLESS there is a negative weight cycle. Why ? ab c d e 6 5 -3 3 2 3 -2 4 What is the least distance from a to e? That is, what is  (a,e)? Take the path

39 The Bellman-Ford Algorithm Solves the single-source shortest-path problem when the weights may be negative. Solves the single-source shortest-path problem when the weights may be negative. It indicates whether or not there is a negative weight cycle reachable from the source vertex. It indicates whether or not there is a negative weight cycle reachable from the source vertex. –If there is, it indicates no solution exists. –If there is not, it returns the solution. It returns FALSE if the graph contains a negative weight cycle that is reachable from the source.

40 Bellman-Ford Algorithm Bellman-Ford(G,w,s) 1. Initialize-Single-Source(G, s) 2. for i  1 to numVertices - 1 3.  (u,v)  E[G] 4. Relax(u,v,w) 5.  (u,v)  E[G] 6. if d[v] > d[u] + w(u,v) 7.return FALSE 8. return TRUE Initialize-Single-Source(G,s) 1. for each vertex v  V[G] 2. d[v]   3.  [v]  NIL 4. d[s]  0 Relax(u,v,w) 1. if d[v] > d[u] + w(u,v) 2. d[v]  d[u] + w(u,v) 3.  [u]  v

41 Example: Bellman-Ford Algorithm 0    7 6 8 -3 9 -2 7 z x t y s -4 2 5

42 Example: Bellman-Ford Algorithm 0 7  6  7 6 8 -3 9 -2 7 z x t y s -4 2 5 Loop order: (t,x), (t, y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

43 Example: Bellman-Ford Algorithm 0 7 46 2 7 6 8 -3 9 -2 7 z x t y s -4 2 5 Loop order: (t,x), (t, y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

44 Example: Bellman-Ford Algorithm 0 7 42 2 7 6 8 -3 9 -2 7 z x t y s -4 2 5 Loop order: (t,x), (t, y), (t,z), (x,t), (y,x), (y,z), (z,x), (z,s), (s,t), (s,y)

45 Three more Applications of Graphs ……

46 Traveling Salesman Problem Math version: G is a complete weighted graph G is a complete weighted graph –Every vertex adjacent to every other Find minimal cycle through all vertices Find minimal cycle through all vertices –A Hamiltonian cycle with minimal weight Going through each vertex only once Going through each vertex only once Intuitive version: We have a map with all cities and distances We have a map with all cities and distances We want to visit each city once (except for origin) We want to visit each city once (except for origin) We want to find minimum distance to travel We want to find minimum distance to travel

47 Three utilities problem Imagine three houses A, B, C and three utilities X, Y, Z If the houses and the utilities are vertices in a graph, is it possible to connect each house to each utility with edges that do not cross one another? The answer : NO Solution for this problem is beyond our scope

48 Four color problem Given a planar graph, can you color the vertices so that no adjacent vertices have the same color, if you have only four colors? Answer is Yes but it is difficult to prove. Planar graph? - where none of its edges cross This problem was posed more than a century before, it was solved in 1970’s with the use of a computer


Download ppt "Graphs Part II Lecture 7. Lecture Objectives  Topological Sort  Spanning Tree  Minimum Spanning Tree  Shortest Path."

Similar presentations


Ads by Google