graph algorithms
play

Graph Algorithms Chapter 22 1 CPTR 430 Algorithms Graph - PowerPoint PPT Presentation

Graph Algorithms Chapter 22 1 CPTR 430 Algorithms Graph Algorithms Why Study Graph Algorithms? Mathematical graphs seem to be relatively specialized and abstract Why spend so much time and effort on algorithms for such an obscure area?


  1. Graph Algorithms Chapter 22 1 CPTR 430 Algorithms Graph Algorithms

  2. Why Study Graph Algorithms? ■ Mathematical graphs seem to be relatively specialized and abstract ■ Why spend so much time and effort on algorithms for such an obscure area? ■ As it turns out, many practical problems can be solved by applying some basic graph algorithms 2 CPTR 430 Algorithms Graph Algorithms

  3. ☎ ✝ ☎ ☎ ☎ ☎ ✆ ☎ ✄ ☎ ✆ ✂ ☎ ✁ � ☎ ✝ ☎ Notation ■ A graph is a mathematical structure that consists of a set of vertices and a set of edges ■ G G E V is the number of vertices ■ E is the number of edges ■ ■ In the context of asymptotic notation, V means V and E means E V 2 2 ■ For example, O really means O V 3 CPTR 430 Algorithms Graph Algorithms

  4. Graph Representation ■ Adjacency matrix ■ Adjacency list 4 CPTR 430 Algorithms Graph Algorithms

  5. Undirected Graph Representations 0 1 2 4 3 0 1 2 3 4 1 4 0 0 0 1 0 0 1 0 4 2 3 1 1 1 0 1 1 1 2 1 3 2 0 1 0 1 0 3 1 4 2 3 0 1 1 0 1 4 3 0 1 4 1 1 0 1 0 5 CPTR 430 Algorithms Graph Algorithms

  6. Directed Graph Representations 0 1 2 4 5 3 0 1 2 3 4 5 1 3 0 0 0 1 0 1 0 0 4 1 1 0 0 0 0 1 0 2 5 4 2 0 0 0 0 1 1 3 1 3 0 1 0 0 0 0 4 3 4 0 0 0 1 0 0 5 5 5 0 0 0 0 0 1 6 CPTR 430 Algorithms Graph Algorithms

  7. ☎ ✝ ☎ ☎ ✆ ☎ � ✝ ☎ ☎ ✆ ☎ ☎ ☎ ☎ ☎ ☎ Which Representation is Better? ■ Adjacency lists are good for sparse graphs 2 ❚ Sparse graph: E V ❚ The list nodes could instead contain pointers to vertices E ❚ For digraphs: sum of lengths of the lists = ❚ For undirected graphs: sum of lengths of the lists = 2 E ❚ Space required: Θ V E ■ Adjacency matrix is better for dense graphs 2 E V ❚ Dense graph: is not mush less than ❚ Can quickly tell if two vertices share an edge ❚ Space required: Θ V 2 , independent of the number of edges ❚ Undirected graphs can use a triangular matrix ❚ Unweighted graphs require only one bit per edge! 7 CPTR 430 Algorithms Graph Algorithms

  8. ✂ ✁ ✆ ✝ ✝ ✂ ✆ ✂ � ✝ ✂ ✆ Weighted Graphs u v E , w : E ■ For each ■ w u v u v is the weight of edge ■ Weighted graphs represent edge costs, lengths, time, etc. ■ Adjacency list: the edge weight is stored in the node for the edge in the list ■ Adjacency matrix: the edge weight is stored in matrix 12 0 1 10 8 2 10 9 8 11 3 4 8 CPTR 430 Algorithms Graph Algorithms

  9. � Breadth-first Search ■ Used in a number of classic graph algorithms: ❚ Prim’s minimal spanning tree ❚ Dijkstra’s single source shortest path ■ Finds all the vertices reachable from a source vertex, s ■ Produces a breadth-first tree whose root is s ■ Computes the length of the path to each reachable vertex Path length = number of edges in the path ■ Breadth-first all the vertices at distance k are located before any reachable vertices farther than distance k are found 9 CPTR 430 Algorithms Graph Algorithms

  10. ✆ ✂ ✝ � � Coloring the Vertices ■ BFS marks vertices to manage its progress ■ Vertices are: ❚ Colored white initially ❚ Colored gray when first discovered ❚ Colored black when all their neighbors have been discovered u v E and u is black v is either gray or black ■ ■ Gray vertices represent the frontier between discovered and undiscovered vertices 0 1 2 3 4 10 CPTR 430 Algorithms Graph Algorithms

  11. � Breadth-first Tree ■ BFS essentially builds a tree ❚ The tree’s root is the source vertex ❚ Vertex v discovered while scanning vertex u neighbors the tree has an edge from parent u to child v 0 1 2 3 4 11 CPTR 430 Algorithms Graph Algorithms

  12. ✁ � ✁ � Implementation Particulars ■ Augment vertices with additional information: ❚ color (black, gray, white) ( color ) ❚ predecessor (parent vertex in the BF tree), π v ( predecessor ) ❚ distance (number of edges from root [source vertex] to this vertex), d v ( distance ) ■ Use a queue to manage the gray vertices 12 CPTR 430 Algorithms Graph Algorithms

  13. ✁ � How It Works ■ First, for each vertex: ❚ set its color to white (undiscovered) ❚ set its predecessor to null (not in the BF tree) ❚ set its distance to ∞ (may not even be a path to it from the source vertex) ■ For the start vertex s : ❚ color s gray ❚ set d s to 0 ❚ place s in the queue 13 CPTR 430 Algorithms Graph Algorithms

  14. ✁ ✁ ✁ � � � � How It Works (cont.) ■ As long as the queue is not empty, extract a vertex u from the queue and: ❚ for each vertex v adjacent to u : ❙ color v gray ❙ set d v to d u 1 ❙ set π v to u ❙ Place v into the queue ❚ Color u black 14 CPTR 430 Algorithms Graph Algorithms

  15. � � ✝ � ✝ ✆ ✆ ✝ ✆ Analysis of Running Time ■ Use aggregate analysis ■ Every vertex must be colored and properly initialized the running time for initialization is O V ■ After initialization, no vertex is ever made white again each vertex is enqueued at most once Unreachable vertices will not be enqueued ■ Recall that the time to enqueue and dequeue elements is O 1 all queue operations take O V time 15 CPTR 430 Algorithms Graph Algorithms

  16. � � � ✆ ✝ ✝ ✆ � ✆ ✆ ✝ ✝ ✆ � ✆ � ✝ ✝ Analysis of Running Time (cont.) ■ Each time a vertex is dequeued: ❚ its adjacent vertices (via its adjacency list) are each are checked to see if they need to be enqueued (i.e., are white) ❚ a vertex’s adjacency list is checked once only ❚ the sum of the vertices in all adjacency lists is Θ E And entry in an adjacency list represents an edge The time spent scanning the adjacency lists is O E the total time for breadth-first search is ■ O V O V O E O V E ■ This means that BFS runs in time linear in the size of the (complete) adjacency list representation of the graph 16 CPTR 430 Algorithms Graph Algorithms

  17. ✆ ✂ ✆ ✂ � ✆ � ✂ ✝ ✝ ✝ ✆ ✝ ✆ ✂ ✝ ✝ � ✝ ✂ ✆ � � � ✂ ✂ ✝ ✆ � ✂ ✝ ✝ ✂ ✆ ✝ � ✁ ✂ ✂ ✂ ✄ ✆ ☎ � � ✆ ✆ ✝ ✝ Shortest Path ■ Define δ s v to be the shortest distance from vertex s to vertex v ❚ The minimum number of edges in any path from s to v ❚ δ ∞ s v no path exists between s and v δ δ u v E s v s u 1 (Lemma 22.1) ■ ❚ u is reachable from s v is reachable from s ❚ The shortest path from s to v cannot be longer than the shortest path from s to u followed by edge u v δ δ s v s u 1 δ ∞ ❚ u is unreachable from s s u δ δ s v s u 1 17 CPTR 430 Algorithms Graph Algorithms

  18. ✂ ✂ � � ✁ � ✝ ✆ ✂ ✆ ✆ ✝ ✝ � ✂ � ✆ � � Shortest Path Property of BFS , BFS assigns δ For any directed or undirected graph G V E s v the distance attribute of each vertex v V δ ■ Proof: First, show something weaker: v V d v s v Lemma 22.2 ■ Proceed by induction on the number of enqueue operations ❚ Basis case: Is it true after the first enqueue operation? ❚ Inductive step: True after k enqueue operations true after k 1 enqueue operations? 18 CPTR 430 Algorithms Graph Algorithms

  19. ✁ ✝ � ✆ ✂ ✂ ✂ ✁ � ✝ � ✆ ✆ � ✂ � ✂ � ✆ ✁ � � � ✁ ✆ � ✂ ✝ � Basis Step This is the case where s , the source vertex, is enqueued Due to the initialization part of the algorithm: δ d s 0 s s and ∞ δ v V s d v s v δ v V d v s v In either situation, 19 CPTR 430 Algorithms Graph Algorithms

  20. ✂ ✂ � ✆ � ✁ � � ✆ ✁ � ✁ � ✝ ✝ ✂ � ✆ � ✝ � � Inductive Step ■ A vertex is enqueued only if it is white ■ Let vertex v be a white vertex discovered while considering vertex u ’s neighbors I.e., u v E , and v is white ■ Vertex v must be enqueued ■ The inductive step is then d v d u 1 Assignment in BFS δ s u 1 I.H. δ s v Lemma 22.1 ■ Once enqueued, v ’s color is set to gray, so it can never be enqueued again d v is never changed again, and the I.H. is maintained 20 CPTR 430 Algorithms Graph Algorithms

  21. � ✂ ✁ ✂ � ✂ � ✝ ✁ � � � ✂ � ✝ ✂ � ✆ ✆ ✁ ✝ ✄ � ✂ � � ✂ � ✁ ✂ ✁ ✁ � Lemma 22.3 v 1 v 2 v r ■ Let be the vertices, in order, in the queue during some V E part of the execution of BFS on graph ■ v 1 is the head of the queue ■ v r is the tail of the queue 1 ■ d v r d v 1 i 1 2 r 1 d v i d v i ■ 1 21 CPTR 430 Algorithms Graph Algorithms

  22. � � Proof of Lemma 22.3 Proceed by induction of the number of queue operations (not just the number of enqueue operations) ■ Basis case: Does the lemma hold after the first queue operation? ■ Inductive step: True after k queue operations true after k 1 queue operations? 22 CPTR 430 Algorithms Graph Algorithms

  23. ✝ ✁ � � ✁ � ✁ ✁ � ✝ ✁ � � ✁ � � ✁ � � Basis Case ■ The first queue operation is always enqueuing s , the source vertex ■ Here, r 1 : ❚ d v r d s d v 1 d v 1 1 ❚ d v i d v i holds vacuously 1 23 CPTR 430 Algorithms Graph Algorithms

Recommend


More recommend