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

Slides:



Advertisements
Similar presentations
Graph Algorithms Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 11Feb 07, 2014Carnegie Mellon University.
Advertisements

What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Graphs - II Algorithms G. Miller V. Adamchik CS Spring 2014 Carnegie Mellon University.
Great Theoretical Ideas in Computer Science for Some.
CSC401 – Analysis of Algorithms Lecture Notes 14 Graph Biconnectivity
Theory of Computing Lecture 6 MAS 714 Hartmut Klauck.
Lecture 16: DFS, DAG, and Strongly Connected Components Shang-Hua Teng.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
CS 312 – Graph Algorithms1 Graph Algorithms Many problems are naturally represented as graphs – Networks, Maps, Possible paths, Resource Flow, etc. Ch.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
16a-Graphs-More (More) Graphs Fonts: MTExtra:  (comment) Symbol:  Wingdings: Fonts: MTExtra:  (comment) Symbol:  Wingdings:
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
1 The Graph Abstract Data Type CS 5050 Chapter 6.
Connected Components, Directed Graphs, Topological Sort COMP171.
© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§6.4) Reachability (§6.4.1) Directed DFS Strong connectivity Transitive.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 10 Instructor: Paul Beame.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Elementary graph algorithms Chapter 22
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
IS 2610: Data Structures Graph April 5, 2004.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
1 ELEC692 Fall 2004 Lecture 1b ELEC692 Lecture 1a Introduction to graph theory and algorithm.
CSCI 115 Chapter 7 Trees. CSCI 115 §7.1 Trees §7.1 – Trees TREE –Let T be a relation on a set A. T is a tree if there exists a vertex v 0 in A s.t. there.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Data Structures & Algorithms Graphs
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
COSC 2007 Data Structures II Chapter 14 Graphs I.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Properties and Applications of Depth-First Search Trees and Forests
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Graphs and Paths : Chapter 15 Saurav Karmakar
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
1 Closures of Relations Based on Aaron Bloomfield Modified by Longin Jan Latecki Rosen, Section 8.4.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO. Directed Graphs2 Outline and Reading (§12.4) Reachability (§12.4.1) Directed DFS Strong connectivity Transitive.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Directed Graphs Directed Graphs Shortest Path 12/7/2017 7:10 AM BOS
Directed Graphs 12/7/2017 7:15 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Directed Graphs 9/20/2018 1:45 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Directed Graphs 5/1/15 12:25:22 PM
Graph Algorithms Using Depth First Search
Directed Graphs Directed Graphs 1 Shortest Path Shortest Path
Biconnectivity SEA PVD ORD FCO SNA MIA 11/16/2018 2:31 AM
Directed Graphs (digraphs)
Planarity Testing.
Elementary graph algorithms Chapter 22
Chapter 9: Graphs Basic Concepts
Lectures on Graph Algorithms: searching, testing and sorting
Directed Graphs Directed Graphs Directed Graphs 2/23/ :12 AM BOS
Biconnectivity SEA PVD ORD FCO SNA MIA 5/6/2019 5:08 PM Biconnectivity
Elementary graph algorithms Chapter 22
Biconnectivity SEA PVD ORD FCO SNA MIA 5/23/ :21 PM
Closures of Relations Epp, section 10.1,10.2 CS 202.
Chapter 9: Graphs Basic Concepts
Presentation transcript:

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

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?

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

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?

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.

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

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.

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.

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?

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]

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.

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…

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

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

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.

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. */

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

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.