Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 361 – Chapter 13 Graph Review Purpose Representation Traversal Comparison with tree.

Similar presentations


Presentation on theme: "CS 361 – Chapter 13 Graph Review Purpose Representation Traversal Comparison with tree."— Presentation transcript:

1 CS 361 – Chapter 13 Graph Review Purpose Representation Traversal Comparison with tree

2 Graph Definition, assumptions Why would we use a graph data structure? –To model something that “looks like” a graph –Relationships between objects we can quantify/measure Representation –Adjacency list –Adjacency matrix (implement as array or ArrayList?) –What about weighted or directed graph?

3 Possible design Graph Attributes –# vertices, # edges –List of vertices –Adjacency matrix Graph Operations –Create empty graph –Add vertex; add edge –getVertex(key) –edgeExists(v1, v2) –degree(v) –Neighbor iterator(v) –BFS & DFS iterators (v) –Is connected, etc. Design of Vertex depends on graph’s application… Vertex attributes –Name –Marked –Level –Others depending on app. Vertex operations –Mark / unmark / is marked –Get and set level –equals

4 Example ABCDEFGH A111 B111 C111 D1111 E111 F111 G111 H11 In this graph, how would we determine: –Degree and neighbors –BFS –DFS –Is the graph connected? –Is the graph bipartite? –Is there a cycle? What does it mean if matrix is symmetric?

5 DFS applications If we can perform a DFS on a graph, doing so also gives us further information : “discovery edges” form a spanning tree Can tell if graph is connected Can tell if graph is cyclic Can find a path between two vertices BFS also gives you this information. BFS is good at finding a shortest path.

6 Directed graphs What does it mean to be cyclic; connected? DFS Traversal Transitive closure –Provides quick way to determine if vertex is reachable Topological ordering –Relevant for Directed Acyclic Graphs –Find a way to appropriately number the vertices

7 Properties In a directed graph, every edge is a 1-way street. Adjacency matrix –Not necessarily symmetric –Each “1” counts as a single directed edge –Slightly different way to determine total number of edges compared to undirected graph –To convert undirected to directed, need to double each edge Raises the bar on what counts as “cyclic” –must have directed cycle –Smallest cycle is round trip to adjacent vertex. “Strongly” connected: analogous to undirected version: For all u, v: each is reachable from the other.

8 DFS Analogous to undirected case. We only travel in the indicated direction. 4 kinds of edges when we’re done: –Discovery = in the DFS spanning tree –Back = to ancestor in the DFS tree –Forward = to descendant in the DFS tree –Cross = to neither ancestor nor descendant Can we create a directed graph having all 4 types of edges? As before, DFS can help answer connectivity questions.

9 Transitive closure We’d like to make shortcuts: Add more edges, so that if v was reachable from u, now we can get there immediately. Creating this 2 nd graph can speed up future queries about reachability. Floyd-Warshall algorithm (p. 377) –Number the vertices 1 to n (can be arbitrary). –We are going to modify the graph n times, once per vertex. –For each vertex v k, we look for all edges of the form (v i, v k ) and (v k, v j ). If we find a match, create edge (v i, v j ). –What is the run-time complexity?

10 Algorithm FloydWarshall(G): Number vertices v1, v2,..., vn. G[0] = G for k = 1 to n: G[k] = G[k-1] for i = 1 to n except k: for j = 1 to n except i,k: if  (vi,vk) and (vk,vj) add (vi,vj) to G[k] if not  already // G[n] is the transitive closure return G[n]

11 DAG Directed Acyclic Graph –Not quite a tree –We can have (undirected) cycle, but not a directed one. –Useful for identifying pre-requisite actions in a series of tasks. –Another application: function call graph in a computer program (as long as there’s no recursion) Topological ordering –Number vertices, telling us the order we may correctly visit the vertices. –For every edge (v i, v j ), i < j. –There could be more than 1 possible ordering. Useful fact:  topological ordering  the directed graph is acyclic.

12 Proof  (by contradiction) Suppose  topological ordering, and  cycle. The cycle includes edges (v k, __) (__, __) (__, __) … (__, v k ) and we’d conclude that k < k. Contradiction  no cycle.  (algorithm) Suppose acyclic. Here’s how to order the vertices. Let v 1 be a vertex with in-degree 0. There must be such a vertex. Remove v 1 and its out edges. Resulting graph still acyclic. Can then find v 2 with in-degree 0. Remove, continue…

13 Biconnected components Problem: given a graph, are there potential bottlenecks? Are there places where there is only 1 path versus 2+ paths between arbitrary vertices? Essential edge (or separation edge) = an edge whose removal disconnects the graph. Essential vertex (or separation vertex). Biconnected means a graph has 2 disjoint paths between any two vertices. –Disjoint paths cannot share any edges or vertices except endpoints. One path can act as detour of the other. Biconnected component = a subgraph that is either: –Just an essential edge with its endpoints (degenerate case), or –Biconnected in a maximal sense: adding any more vertices or edges renders it no longer biconnected. See figure on p. 386

14 Look at edges We want to find the biconnected components in a graph. The key is to look at the edges. Define a link relation on edges: 2 edges are “linked” if –They are the same edge, or –Some simple cycle* contains both edges. * Simple cycle: the only vertices that repeat are the endpoints. This link relation is an equivalence relation. Equivalence classes correspond to biconnected components. –Justification is Lemma 13.23: –Being biconnected  any 2 vertices lie along a simple cycle. –(Being biconnected  no essential vertices or edges.)

15 Approach Theoretically, to find the biconnected components (i.e. “link components”), we can find the equivalence classes of the edges. See which edges belong to the same cycle. (p. 388) How to do it: –Find the DFS of the original graph G. –Create a 2 nd graph “B” based on G. Edges of G become the vertices of B! Edges of B? For each back edge e in G’s DFS spanning tree, find the discovery edges f i in same simple cycle as e. Create an edge in B (e, f i ) for each f i. –Because the simple cycles in G probably don’t overlap, we’d expect B to be disconnected. Return the connected components of B. We can save time by noting that we don’t need all of G’s edges.

16 Algorithm Find biconnected/link components of G: Find G’s DFS traversal. Take note of discovery and back edges, and the order of visiting vertices. Create 2 nd graph F. Discovery edges in G become vertices of F. Label all vertices in F as “unlinked”. /* Note: # discovery edges = n-1. Names of edges should be based on the order of direction as we created discovery/back edge. For example, “JF” would be a back edge. For book’s example, A-M visited in that order, so every back edge’s name is in reverse alpha order. */

17 Algorithm (2) for each vertex v in G: for each back edge e = (u,v): Add e as new vertex in F. while u ≠ v, we march up: Find vertex f  F matching discovery edge: u’s parent, u) in DFS tree Add edge (e,f) to F, where e = current back edge and f is discovery edge just found. if f unlinked, mark f as “linked” u = parent(u) else break // avoid repetition

18 Example Book’s example G has vertices A-M. Corresponding graph F has 12 vertices: GH, HI, DE, FG, HJ, CD, CF, BC, CM, CK, KL, AB. Begin nested loop! –Vertex A has back edges KA and MA to it. Look at KA first. –In DFS tree we see A  B  C  K. So, v=A and u=K. Add KA to graph F. u=K and v=A not equal. Discovery edge is f = CK. Draw edge in F from KA to CK. Mark CK linked. u=C and v=A not equal. Discovery edge is f = BC. Draw edge in F from KA to BC. Mark BC linked. u=B and v=A not equal. Discovery edge is f = AB. Draw edge in F from KA to AB. Mark AB linked. u=A and v=A equal. Exit loop. –At this point, we look at other back edge MA. –… Final answer (p. 391) shows 5 components.


Download ppt "CS 361 – Chapter 13 Graph Review Purpose Representation Traversal Comparison with tree."

Similar presentations


Ads by Google