Introduction to Algorithms Graph Algorithms CSE 680 Prof

  • Slides: 63
Download presentation
Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io. uwinnipeg.

Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io. uwinnipeg. ca/~ychen 2

Graphs w Graph G = (V, E) » V = set of vertices »

Graphs w Graph G = (V, E) » V = set of vertices » E = set of edges (V V) w Types of graphs » Undirected: edge (u, v) = (v, u); for all v, (v, v) E (No self loops. ) » Directed: (u, v) is edge from u to v, denoted as u v. Self loops are allowed. » Weighted: each edge has an associated weight, given by a weight function w : E R. » Dense: |E| |V|2. » Sparse: |E| << |V|2. w |E| = O(|V|2) graphs-1 - 2

Graphs w If (u, v) E, then vertex v is adjacent to vertex u.

Graphs w If (u, v) E, then vertex v is adjacent to vertex u. w Adjacency relationship is: » Symmetric if G is undirected. » Not necessarily so if G is directed. w If G is connected: » There is a path between every pair of vertices. » |E| |V| – 1. » Furthermore, if |E| = |V| – 1, then G is a tree. w Other definitions in Appendix B (B. 4 and B. 5) as needed. graphs-1 - 3

Representation of Graphs w Two standard ways. » Adjacency Lists. a b c d

Representation of Graphs w Two standard ways. » Adjacency Lists. a b c d a b d b a c c d d a a c » Adjacency Matrix. 1 3 graphs-1 - 4 a b c d 2 4 1 2 3 4 1 0 1 1 1 2 1 0 3 1 1 0 1 4 1 0 c b

Adjacency Lists w Consists of an array Adj of |V| lists. w One list

Adjacency Lists w Consists of an array Adj of |V| lists. w One list per vertex. w For u V, Adj[u] consists of all vertices adjacent to u. a b c c d b c d a b d a c d b c d d a a c c graphs-1 - 5 d c If weighted, store weights also in adjacency lists. d c b

Storage Requirement w For directed graphs: » Sum of lengths of all adj. lists

Storage Requirement w For directed graphs: » Sum of lengths of all adj. lists is out-degree(v) = |E| v V No. of edges leaving v » Total storage: (|V| + |E|) w For undirected graphs: » Sum of lengths of all adj. lists is degree(v) = 2|E| v V No. of edges incident on v. Edge (u, v) is incident on vertices u and v. » Total storage: (|V| + |E|) graphs-1 - 6

Pros and Cons: adj list w Pros » Space-efficient, when a graph is sparse.

Pros and Cons: adj list w Pros » Space-efficient, when a graph is sparse. » Can be modified to support many graph variants. w Cons » Determining if an edge (u, v) G is not efficient. • Have to search in u’s adjacency list. (degree(u)) time. • (V) in the worst case. graphs-1 - 7

Adjacency Matrix w |V| matrix A. w Number vertices from 1 to |V| in

Adjacency Matrix w |V| matrix A. w Number vertices from 1 to |V| in some arbitrary manner. w A is then given by: 1 3 2 a b c d 4 1 2 3 4 1 0 0 2 1 0 0 0 3 1 1 0 0 4 1 0 1 3 a b c d 2 4 1 2 3 4 1 0 1 1 1 2 1 0 3 1 1 0 1 4 1 0 A = AT for undirected graphs-1 - 8

Space and Time w Space: (V 2). » Not memory efficient for large graphs.

Space and Time w Space: (V 2). » Not memory efficient for large graphs. w Time: to list all vertices adjacent to u: (V). w Time: to determine if (u, v) E: (1). w Can store weights instead of bits for weighted graphs-1 - 9

Some graph operations adjacency matrix adjacency lists insert. Edge O(1) O(e) is. Edge O(1)

Some graph operations adjacency matrix adjacency lists insert. Edge O(1) O(e) is. Edge O(1) O(e) #successors? O(V) O(e) #predecessors? O(V) O(E) graphs-1 - 10

traversing a graph ny bos dc la chi atl Where to start? Will all

traversing a graph ny bos dc la chi atl Where to start? Will all vertices be visited? How to prevent multiple visits? graphs-1 - 11

Graph Definitions w Path » Sequence of nodes n 1, n 2, … nk

Graph Definitions w Path » Sequence of nodes n 1, n 2, … nk » Edge exists between each pair of nodes ni , ni+1 » Example • A, B, C is a path graphs-1 - 12

Graph Definitions w Path » Sequence of nodes n 1, n 2, … nk

Graph Definitions w Path » Sequence of nodes n 1, n 2, … nk » Edge exists between each pair of nodes ni , ni+1 » Example • A, B, C is a path • A, E, D is not a path graphs-1 - 13

Graph Definitions w Cycle » Path that ends back at starting node » Example

Graph Definitions w Cycle » Path that ends back at starting node » Example • A, E, A graphs-1 - 14

Graph Definitions w Cycle » Path that ends back at starting node » Example

Graph Definitions w Cycle » Path that ends back at starting node » Example • A, E, A • A, B, C, D, E, A w Simple path » No cycles in path w Acyclic graph » No cycles in graphs-1 - 15

Graph Definitions w Reachable » Path exists between nodes w Connected graph » Every

Graph Definitions w Reachable » Path exists between nodes w Connected graph » Every node is reachable from some node in graph Unconnected graphs-1 - 16

Graph-searching Algorithms w Searching a graph: » Systematically follow the edges of a graph

Graph-searching Algorithms w Searching a graph: » Systematically follow the edges of a graph to visit the vertices of the graph. w Used to discover the structure of a graph. w Standard graph-searching algorithms. » Breadth-first Search (BFS). » Depth-first Search (DFS). graphs-1 - 17

Breadth-first Search w Input: Graph G = (V, E), either directed or undirected, and

Breadth-first Search w Input: Graph G = (V, E), either directed or undirected, and source vertex s V. w Output: » d[v] = distance (smallest # of edges, or shortest path) from s to v, for all v V. d[v] = if v is not reachable from s. » [v] = u such that (u, v) is last edge on shortest path s v. • u is v’s predecessor. » Builds breadth-first tree with root s that contains all reachable vertices. graphs-1 - 18

Breadth-first Search w Expands the frontier between discovered and undiscovered vertices uniformly across the

Breadth-first Search w Expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier. » A vertex is “discovered” the first time it is encountered during the search. » A vertex is “finished” if all vertices adjacent to it have been discovered. w Colors the vertices to keep track of progress. » White – Undiscovered. » Gray – Discovered but not finished. » Black – Finished. graphs-1 - 19

BFS(G, s) 1. for each vertex u in V[G] – {s} 2 do color[u]

BFS(G, s) 1. for each vertex u in V[G] – {s} 2 do color[u] white initialization 3 d[u] 4 [u] nil 5 color[s] gray 6 d[s] 0 access source s 7 [s] nil 8 Q 9 enqueue(Q, s) 10 while Q 11 do u dequeue(Q) 12 for each v in Adj[u] 13 do if color[v] = white 14 then color[v] gray 15 d[v] d[u] + 1 16 [v] u 17 enqueue(Q, v) 18 color[u] black graphs-1 - 21 white: undiscovered gray: discovered black: finished Q: a queue of discovered vertices color[v]: color of v d[v]: distance from s to v [u]: predecessor of v

Example (BFS) r 0 v w s u y x Q: s 0 graphs-1

Example (BFS) r 0 v w s u y x Q: s 0 graphs-1 - 22 t

Example (BFS) r s 1 0 v 1 w t u y x Q:

Example (BFS) r s 1 0 v 1 w t u y x Q: w r 1 1 graphs-1 - 23

Example (BFS) r s 1 0 v 1 w t 2 u 2 y

Example (BFS) r s 1 0 v 1 w t 2 u 2 y x Q: r t x 1 2 2 graphs-1 - 24

Example (BFS) r s 1 0 2 v 1 w t 2 u 2

Example (BFS) r s 1 0 2 v 1 w t 2 u 2 y x Q: t x v 2 2 2 graphs-1 - 25

Example (BFS) r s 1 0 2 v 1 w t 2 u 3

Example (BFS) r s 1 0 2 v 1 w t 2 u 3 2 y x Q: x v u 2 2 3 graphs-1 - 26

Example (BFS) r s 1 0 2 v 1 w t 2 u 3

Example (BFS) r s 1 0 2 v 1 w t 2 u 3 2 3 y x Q: v u y 2 3 3 graphs-1 - 27

Example (BFS) r s 1 0 2 v 1 w u 3 2 3

Example (BFS) r s 1 0 2 v 1 w u 3 2 3 y x Q: u y 3 3 graphs-1 - 28 t 2

Example (BFS) r s 1 0 2 v 1 w u 3 2 3

Example (BFS) r s 1 0 2 v 1 w u 3 2 3 y x Q: y 3 graphs-1 - 29 t 2

Example (BFS) r s 1 0 2 v 1 w u 3 2 3

Example (BFS) r s 1 0 2 v 1 w u 3 2 3 y x Q: graphs-1 - 30 t 2

Example (BFS) r s 1 0 2 v 1 w u 3 2 3

Example (BFS) r s 1 0 2 v 1 w u 3 2 3 y x BF Tree graphs-1 - 31 t 2

Analysis of BFS w Initialization takes O(|V|). w Traversal Loop » After initialization, each

Analysis of BFS w Initialization takes O(|V|). w Traversal Loop » After initialization, each vertex is enqueued and dequeued at most once, and each operation takes O(1). So, total time for queuing is O(|V|). » The adjacency list of each vertex is scanned at most once. The sum of lengths of all adjacency lists is (|E|). w Summing up over all vertices => total running time of BFS is O(|V| + |E|), linear in the size of the adjacency list representation of graphs-1 - 32

Breadth-first Tree w For a graph G = (V, E) with source s, the

Breadth-first Tree w For a graph G = (V, E) with source s, the predecessor subgraph of G is G = (V , E ) where » V ={v V : [v] nil} {s} » E ={( [v], v) E : v V - {s}} w The predecessor subgraph G is a breadth-first tree if: » V consists of the vertices reachable from s and » for all v V , there is a unique simple path from s to v in G that is also a shortest path from s to v in G. w The edges in E are called tree edges. |E | = |V | - 1. graphs-1 - 33

Depth-first Search (DFS) w Explore edges out of the most recently discovered vertex v.

Depth-first Search (DFS) w Explore edges out of the most recently discovered vertex v. w When all edges of v have been explored, backtrack to explore other edges leaving the vertex from which v was discovered (its predecessor). w “Search as deep as possible first. ” w Continue until all vertices reachable from the original source are discovered. w If any undiscovered vertices remain, then one of them is chosen as a new source and search is repeated from that source. graphs-1 - 34

Depth-first Search w Input: G = (V, E), directed or undirected. No source vertex

Depth-first Search w Input: G = (V, E), directed or undirected. No source vertex given! w Output: » 2 timestamps on each vertex. Integers between 1 and 2|V|. • d[v] = discovery time (v turns from white to gray) • f [v] = finishing time (v turns from gray to black) » [v] : predecessor of v = u, such that v was discovered during the scan of u’s adjacency list. w Coloring scheme for vertices as BFS. A vertex is » “discovered” the first time it is encountered during the search. » A vertex is “finished” if it is a leaf node or all vertices adjacent to it have been finished. graphs-1 - 35

Pseudo-code DFS(G) 1. for each vertex u V[G] 2. do color[u] white 3. [u]

Pseudo-code DFS(G) 1. for each vertex u V[G] 2. do color[u] white 3. [u] NIL 4. time 0 5. for each vertex u V[G] 6. do if color[u] = white 7. then DFS-Visit(u) Uses a global timestamp time. graphs-1 - 36 DFS-Visit(u) 1. color[u] GRAY // White vertex u has been discovered 2. time + 1 3. d[u] time 4. for each v Adj[u] 5. do if color[v] = WHITE 6. then [v] u 7. DFS-Visit(v) 8. color[u] BLACK // Blacken u; it is finished. 9. f[u] time + 1

Example (DFS) u v w y z 1/ x graphs-1 - 37

Example (DFS) u v w y z 1/ x graphs-1 - 37

Example (DFS) u graphs-1 - 38 w 1/ v 2/ x y z

Example (DFS) u graphs-1 - 38 w 1/ v 2/ x y z

Example (DFS) u graphs-1 - 39 1/ v 2/ x 3/ y w z

Example (DFS) u graphs-1 - 39 1/ v 2/ x 3/ y w z

Example (DFS) u graphs-1 - 40 1/ v 2/ 4/ x 3/ y w

Example (DFS) u graphs-1 - 40 1/ v 2/ 4/ x 3/ y w z

Example (DFS) u v 2/ 1/ w B 4/ x graphs-1 - 41 3/

Example (DFS) u v 2/ 1/ w B 4/ x graphs-1 - 41 3/ y z

Example (DFS) u v 2/ 1/ w B 4/5 x graphs-1 - 42 3/

Example (DFS) u v 2/ 1/ w B 4/5 x graphs-1 - 42 3/ y z

Example (DFS) u v 2/ 1/ w B 4/5 x graphs-1 - 43 3/6

Example (DFS) u v 2/ 1/ w B 4/5 x graphs-1 - 43 3/6 y z

Example (DFS) u v 2/7 1/ w B 4/5 x graphs-1 - 44 3/6

Example (DFS) u v 2/7 1/ w B 4/5 x graphs-1 - 44 3/6 y z

Example (DFS) u v 2/7 1/ F 4/5 x graphs-1 - 45 w B

Example (DFS) u v 2/7 1/ F 4/5 x graphs-1 - 45 w B 3/6 y z

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 46 w B

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 46 w B 3/6 y z

Example (DFS) u 1/8 F 4/5 x graphs-1 - 47 v 2/7 w 9/

Example (DFS) u 1/8 F 4/5 x graphs-1 - 47 v 2/7 w 9/ 3/6 y z B

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 48 B w

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 48 B w 9/ C 3/6 y z

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 49 B w

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 49 B w 9/ C 3/6 y 10/ z

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 50 B w

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 50 B w 9/ C 3/6 y 10/ z B

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 51 B w

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 51 B w 9/ C 3/6 y 10/11 z B

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 52 B w

Example (DFS) u v 2/7 1/8 F 4/5 x graphs-1 - 52 B w 9/12 C 3/6 y 10/11 z B

Analysis of DFS w Loops on lines 1 -2 & 5 -7 take (V)

Analysis of DFS w Loops on lines 1 -2 & 5 -7 take (V) time, excluding time to execute DFS-Visit. w DFS-Visit is called once for each white vertex v V when it’s painted gray the first time. Lines 3 -6 of DFSVisit is executed |Adj[v]| times. The total cost of executing DFS-Visit is v V|Adj[v]| = (E) w Total running time of DFS is (|V| + |E|). graphs-1 - 53

Recursive DFS Algorithm Traverse( ) for all nodes X visited[X]= False DFS( 1 st

Recursive DFS Algorithm Traverse( ) for all nodes X visited[X]= False DFS( 1 st node ) DFS( X ) visited[X] = True for each successor Y of X if (visited[Y] = False) DFS( Y ) graphs-1 - 54

Parenthesis Theorem 22. 7 For all u, v, exactly one of the following holds:

Parenthesis Theorem 22. 7 For all u, v, exactly one of the following holds: 1. d[u] < f [u] < d[v] < f [v] or d[v] < f [v] < d[u] < f [u] and neither u nor v is a descendant of the other. 2. d[u] < d[v] < f [u] and v is a descendant of u. 3. d[v] < d[u] < f [v] and u is a descendant of v. w So d[u] < d[v] < f [u] < f [v] cannot happen. w Like parentheses: w OK: ( ) [ ] ( [ ] ) [ ( ) ] w Not OK: ( [ ) ] [ ( ] ) ( d[v] ) f[v] f[u] d[u] ) ( ] [ f[v] d[v] Corollary v is a proper descendant of u if and only if d[u] < d[v] < f [u]. graphs-1 - 55

Example (Parenthesis Theorem) y z 2/9 3/6 B 4/5 x C F 7/8 w

Example (Parenthesis Theorem) y z 2/9 3/6 B 4/5 x C F 7/8 w t s 1/10 C 11/16 C 12/13 v B C 14/15 u (s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t) graphs-1 - 56

Depth-First Trees w Predecessor subgraph defined slightly different from that of BFS. w The

Depth-First Trees w Predecessor subgraph defined slightly different from that of BFS. w The predecessor subgraph of DFS is G = (V, E ) where E ={( [v], v) : v V and [v] nil}. » How does it differ from that of BFS? » The predecessor subgraph G forms a depth-first forest composed of several depth-first trees. The edges in E are called tree edges. Definition: Forest: An acyclic graph G that may be disconnected. graphs-1 - 57

White-path Theorem 22. 9 v is a descendant of u in DF-tree if and

White-path Theorem 22. 9 v is a descendant of u in DF-tree if and only if at time d[u], there is a path u v consisting of only white vertices. (Except for u, which was just colored gray. ) graphs-1 - 58

Classification of Edges w Tree edge: in the depth-first forest. Found by exploring (u,

Classification of Edges w Tree edge: in the depth-first forest. Found by exploring (u, v). w Back edge: (u, v), where u is a descendant of v (in the depth-first tree). w Forward edge: (u, v), where v is a descendant of u, but not a tree edge. w Cross edge: any other edge. Can go between vertices in same depth-first tree or in different depth-first trees. Theorem: In DFS of an undirected graph, we get only tree and back edges. No forward or cross edges. graphs-1 - 59

Classifying edges of a digraph w (u, v) is: » Tree edge – if

Classifying edges of a digraph w (u, v) is: » Tree edge – if v is white » Back edge – if v is gray » Forward or cross - if v is black w (u, v) is: » Forward edge – if v is black and d[u] < d[v] (v was discovered after u) » Cross edge – if v is black and d[u] > d[v] (u discovered after v) graphs-1 - 60 60

More applications w Does directed G contain a directed cycle? Do DFS if back

More applications w Does directed G contain a directed cycle? Do DFS if back edges yes. Time O(V+E). w Does undirected G contain a cycle? Same as directed but be careful not to consider (u, v) and (v, u) a cycle. Time O(V) since encounter at most |V| edges (if (u, v) and (v, u) are counted as one edge), before cycle is found. w Is undirected G a tree? Do dfs. Visit(v). If all vertices are reached and no back edges G is a tree. O(V) graphs-1 - 61 61

C# Interfaces using System; using System. Collections. Generic; using System. Security. Permissions; [assembly: CLSCompliant(true)]

C# Interfaces using System; using System. Collections. Generic; using System. Security. Permissions; [assembly: CLSCompliant(true)] namespace Ohio. State. Collections. Graph { /// <summary> /// IEdge provides a standard interface to specify an edge and any /// data associated with an edge within a graph. /// </summary> /// <typeparam name="N">The type of the nodes in the graph. </typeparam> /// <typeparam name="E">The type of the data on an edge. </typeparam> public interface IEdge<N, E> { /// <summary> /// Get the Node label that this edge emanates from. /// </summary> N From { get; } /// <summary> /// Get the Node label that this edge terminates at. /// </summary> N To { get; } /// <summary> /// Get the edge label for this edge. /// </summary> E Value { get; } } graphs-1 - 62 /// <summary> /// The Graph interface /// </summary> /// <typeparam name="N">The type associated at each node. Called a node or node label</typeparam> /// <typeparam name="E">The type associated at each edge. Also called the edge label. </typeparam> public interface IGraph<N, E> { /// <summary> /// Iterator for the nodes in the graoh. /// </summary> IEnumerable<N> Nodes { get; } /// <summary> /// Iterator for the children or neighbors of the specified node. /// </summary> /// <param name="node">The node. </param> /// <returns>An enumerator of nodes. </returns> IEnumerable<N> Neighbors(N node); /// <summary> /// Iterator over the parents or immediate ancestors of a node. /// </summary> /// <remarks>May not be supported by all graphs. </remarks> /// <param name="node">The node. </param> /// <returns>An enumerator of nodes. </returns> IEnumerable<N> Parents(N node);

C# Interfaces /// <summary> /// Iterator over the emanating edges from a node. ///

C# Interfaces /// <summary> /// Iterator over the emanating edges from a node. /// </summary> /// <param name="node">The node. </param> /// <returns>An enumerator of nodes. </returns> IEnumerable<IEdge<N, E>> Out. Edges(N node); /// <summary> /// Iterator over the in-coming edges of a node. /// </summary> /// <remarks>May not be supported by all graphs. </remarks> /// <param name="node">The node. </param> /// <returns>An enumerator of edges. </returns> IEnumerable<IEdge<N, E>> In. Edges(N node); /// <summary> /// Iterator for the edges in the graph, yielding IEdge's /// </summary> IEnumerable<IEdge<N, E>> Edges { get; } /// <summary> /// Tests whether an edge exists between two nodes. /// </summary> /// <param name="from. Node">The node that the edge emanates from. </param> /// <param name="to. Node">The node that the edge terminates at. </param> /// <returns>True if the edge exists in the graph. False otherwise. </returns> bool Contains. Edge(N from. Node, N to. Node); /// <summary> /// Gets the label on an edge. /// </summary> /// <param name="from. Node">The node that the edge emanates from. </param> /// <param name="to. Node">The node that the edge terminates at. </param> graphs-1 - 63 /// <returns>The edge. </returns> E Get. Edge. Label(N from. Node, N to. Node); /// <summary> /// Exception safe routine to get the label on an edge. /// </summary> /// <param name="from. Node">The node that the edge emanates from. </param> /// <param name="to. Node">The node that the edge terminates at. </param> /// <param name="edge">The resulting edge if the method was successful. A default /// value for the type if the edge could not be found. </param> /// <returns>True if the edge was found. False otherwise. </returns> bool Try. Get. Edge. Label(N from. Node, N to. Node, out E edge. Label); } }

C# Interfaces using System; namespace Ohio. State. Collections. Graph { /// <summary> /// Graph

C# Interfaces using System; namespace Ohio. State. Collections. Graph { /// <summary> /// Graph interface for graphs with finite size. /// </summary> /// <typeparam name="N">The type associated at each node. Called a node or node label</typeparam> /// <typeparam name="E">The type associated at each edge. Also called the edge label. </typeparam> /// <seealso cref="IGraph{N, E}"/> public interface IFinite. Graph<N, E> : IGraph<N, E> { /// <summary> /// Get the number of edges in the graph. /// </summary> int Number. Of. Edges { get; } /// <summary> /// Get the number of nodes in the graph. /// </summary> int Number. Of. Nodes { get; } } } graphs-1 - 64