Graphs1 Graphs Chapter 6 ORD DFW SFO LAX 802 1743 1843 1233 337.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
Advertisements

Directed Graphs Directed Graphs Shortest Path 4/10/ :45 AM BOS
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Depth-First Search1 DB A C E. 2 Outline and Reading Definitions (§6.1) Subgraph Connectivity Spanning trees and forests Depth-first search (§6.3.1) Algorithm.
CSC 213 ORD DFW SFO LAX Lecture 20: Graphs.
© 2004 Goodrich, Tamassia Graphs1 ORD DFW SFO LAX
1 The Graph Abstract Data Type CS 5050 Chapter 6.
© 2004 Goodrich, Tamassia Directed Graphs1 JFK BOS MIA ORD LAX DFW SFO.
1 Graphs ORD DFW SFO LAX Many slides taken from Goodrich, Tamassia 2004.
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
1 Directed Graphs CSC401 – Analysis of Algorithms Lecture Notes 15 Directed Graphs Objectives: Introduce directed graphs and weighted graphs Present algorithms.
TTIT33 Alorithms and Optimization – DALG Lecture 4 Graphs HT TTIT33 Algorithms and optimization Algorithms Lecture 4 Graphs.
1 Graphs: Concepts, Representation, and Traversal CSC401 – Analysis of Algorithms Lecture Notes 13 Graphs: Concepts, Representation, and Traversal Objectives:
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.
Graphs1 Part-H1 Graphs ORD DFW SFO LAX
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
Graphs1 ORD DFW SFO LAX Graphs2 Outline and Reading Graphs (§6.1) Definition Applications Terminology Properties ADT Data structures.
CSC311: Data Structures 1 Chapter 13: Graphs I Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
ECE669 L10: Graph Applications March 2, 2004 ECE 669 Parallel Computer Architecture Lecture 10 Graph Applications.
TDDB56 DALGOPT-D TDDB57 DALG-C – Lecture 11 – Graphs Graphs HT TDDB56 – DALGOPT-D Algorithms and optimization Lecture 11 Graphs.
1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
Graphs – ADTs and Implementations ORD DFW SFO LAX
Graphs Chapter 12.
Graphs Part 1. Outline and Reading Graphs (§13.1) – Definition – Applications – Terminology – Properties – ADT Data structures for graphs (§13.2) – Edge.
October 22, DFS, BFS, Biconnectivity, Digraphs 1 CS 221 West Virginia University.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
Chapter 6 Graphs ORD DFW SFO LAX
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
Spring 2007Graphs1 ORD DFW SFO LAX
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
© 2004 Goodrich, Tamasia Recall: Digraphs A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights.
October 21, Graphs1 Graphs CS 221 (slides from textbook author)
Depth-First Search1 DB A C E. 2 Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G – Visits all the vertices.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
CSC401: Analysis of Algorithms 6-1 CSC401 – Analysis of Algorithms Chapter 6 Graphs Objectives: Introduce graphs and data structures Discuss the graph.
Graphs Quebec Toronto Montreal Ottawa 449 km 255 km 200 km 545 km Winnipeg 2075 km 2048 km New York 596 km 790 km 709 km.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
1 COMP9024: Data Structures and Algorithms Week Eleven: Graphs (I) Hui Wu Session 1, 2016
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.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
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,
Graphs.
Graphs 5/14/ :46 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs 7/18/2018 7:39 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Graphs ORD SFO LAX DFW Graphs Graphs
COMP9024: Data Structures and Algorithms
Graphs ORD SFO LAX DFW Graphs Graphs
COMP9024: Data Structures and Algorithms
Chapter 14 Graph Algorithms
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
Directed Graphs Directed Graphs 1 Shortest Path Shortest Path
Graphs Part 1.
Graphs ORD SFO LAX DFW Graphs Graphs
Directed Graphs (digraphs)
Graphs.
Graphs.
Depth-First Search D B A C E Depth-First Search Depth-First Search
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs CSE 2011 Winter November 2018.
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs ORD SFO LAX DFW Graphs Graphs
Graphs 4/29/15 01:28:20 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Directed Graphs Directed Graphs Directed Graphs 2/23/ :12 AM BOS
Graphs Part 1 ORD SFO LAX DFW
Graphs ORD SFO LAX DFW /15/ :57 AM
Presentation transcript:

Graphs1 Graphs Chapter 6 ORD DFW SFO LAX

Graphs2 Graph A graph is a pair (V, E), where –V is a set of nodes, called vertices –E is a collection (can be duplicated) of pairs of vertices, called edges –Vertices and edges are data structures and store elements Example: –A vertex represents an airport and stores the three-letter airport code –An edge represents a flight route between two airports and stores the mileage of the route ORD PVD MIA DFW SFO LAX LGA HNL

Graphs3 Edge Types Directed edge –ordered pair of vertices (u,v) –first vertex u is the source or origin –second vertex v is the destination –e.g., a flight Undirected edge –unordered pair of vertices (u,v) –e.g., a flight route Directed graph (digraph) –all the edges are directed –e.g., flight network Undirected graph –all the edges are undirected –e.g., route network ORDPVD flight AA 1206 ORDPVD 849 miles

Graphs4 Applications Electronic circuits –Printed circuit board –Integrated circuit Transportation networks –Highway network –Flight network Computer networks –Local area network –Internet –Web Databases –Entity-relationship diagram

Graphs5 Terminology End vertices (or endpoints) of an edge –U and V are the endpoints of a Edges incident on a vertex –a, d, and b are incident on V Adjacent vertices – share edge –U and V are adjacent Degree of a vertex –X has degree 5 Parallel edges (go between same nodes) –h and i are parallel edges Self-loop (same nodes is origin/destination) –j is a self-loop XU V W Z Y a c b e d f g h i j

Graphs6 P1P1 Terminology (cont.) Simple graphs have no parallel (multiple edges between same vertices) or self loop Path –sequence of alternating vertices and edges which match up Cycle - path with same first and last vertex Simple path –path such that all its vertices and edges are distinct Examples –P 1 =(V,b,X,h,Z) is a simple path –P 2 =(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple XU V W Z Y a c b e d f g hP2P2

Graphs7 Terminology (cont.) Simple cycle –cycle such that all its vertices and edges are distinct Connected graph/component (Spanning – contains all vertices) subgraph Forest (acyclic), free (no root) trees (connected forest), spanning tree Examples –C 1 =(V,b,X,g,Y,f,W,c,U,a,  ) is a simple cycle –C 2 =(U,c,W,e,X,g,Y,f,W,d,V,a,  ) is a cycle that is not simple C1C1 XU V W Z Y a c b e d f g hC2C2

Graphs8 Properties Notation nnumber of vertices mnumber of edges deg(v)degree of vertex v Property 1  v deg(v)  2m Proof: each edge is counted twice Property 2 In an undirected graph with no self-loops and no multiple edges m  n (n  1)  2 Proof: each vertex has degree at most (n  1) What is the bound for a directed graph? Example n  4 m  6 deg(v)  3

Graphs9 Main Methods of the Graph ADT Vertices and edges –are positions –store elements Accessor methods –aVertex() –incidentEdges(v) –endVertices(e) –isDirected(e) –origin(e) –destination(e) –opposite(v, e) –areAdjacent(v, w) Update methods –insertVertex(o) –insertEdge(v, w, o) –insertDirectedEdge(v, w, o) –removeVertex(v) –removeEdge(e) Generic methods –numVertices() –numEdges() –vertices() –edges()

Edge List Structure We could just store a list of nodes and a list of edges. Vertex info: name, id, degree, … Edge info: directed?, endpoints, weight It wouldn’t be all that convenient for doing a traversal, but if our operations involved looking at each edge or each node, it would be simple. Note: edges point to vertexes (not visa versa) 10

Graphs11 Edge List Structure Vertex object –element –reference to position in vertex sequence Edge object –element –origin vertex object –destination vertex object –reference to position in edge sequence Vertex sequence –sequence of vertex objects Edge sequence –sequence of edge objects v u w ac b a z d uv w z bcd

Adjacency List Structure Edge list structure Incidence sequence for each vertex –sequence of references to edge objects of incident edges Augmented edge objects –references to end vertices 12

Graphs13 Adjacency Matrix Structure Edge list structure Augmented vertex objects –Integer key (index) associated with vertex 2D adjacency array –Reference to edge object for adjacent vertices –Null for non nonadjacent vertices The “old fashioned” version just has 0 for no edge and 1 for edge u v w ab  1  2  a uv w 01 2 b

Graphs14 Asymptotic Performance n vertices, m edges no parallel edges no self-loops Bounds are “big-Oh” Edge List Adjacency List Adjacency Matrix Space n  m n2n2 incidentEdges( v ) mdeg(v)n areAdjacent ( v, w ) mmin(deg(v), deg(w))1 insertVertex( o ) 11n2n2 insertEdge( v, w, o ) 111 removeVertex( v ) mdeg(v)n2n2 removeEdge( e ) 111

15 Traversals Visit each node – e.g., web crawler Have to restart traversal in each connected component, but this allows us to identify components Reachability in a digraph is an important issue – the transitive closure graph Book permits counter-direction motion in general traversals

What is meant by depth first search? Go deeper rather than broader Requires recursion or stack Used as a general means of traversal 16

Code for adjacency list implementation class Node { int ID; String nodeLabel; EdgeList adj; // successors public String toString() { return ID + nodeLabel ; } 17 class Edge { int from; // endpoint of start node int to; // endpoint of end node } class EdgeList { EdgeList next; Edge e; } public class Graph{ final static int SIZE = 20; Node [] nodes = new Node[SIZE]; }

At your seats Write the code to visit every node and print its name in the order it is visited. Note that with graphs, you may find yourself going in circles if you don’t mark the nodes as “visited” in some way. 18

19 Looking for a spanning Tree Spanning tree: all of nodes and some of edges. Like a calling tree – make sure all are informed Many times algorithms depend on some ordering of the nodes of a graph Labeling the edges is some way is helpful in organizing thinking about a graph

20 Depth-First Search (undirected graph) Simple recursive backtracking algorithm calls recursive function at starting point –For each incident edge to a vertex If opposite (other) vertex is unvisited –Label edge as “discovery” –Recur on the opposite vertex Else label edge as “back” Discovery edges form a component spanning tree, back edges go to an ancestor with m edges, O(m) using an adjacency list, but not using an adjacency matrix

Depth First Search Notice in the next diagram that multiple steps are shown between each set of pictures. Discovery edges (tree edges) are drawn with solid black lines. Back edges are drawn with dashed lines. The current node is shown in solid black 21

22 Depth-First Traversal

Given this code, find dfs numbers class Node { int ID; int dfsNum; String nodeLabel; EdgeList adj; // successors public String toString() { return ID + nodeLabel ; } 23 class Edge { int from; // endpoint of start node int to; // endpoint of end node int edgeType: {BACK, TREE, CROSS, FORWARD} } class EdgeList { EdgeList next; Edge e; } public class Graph{ final static int SIZE = 20; Node [] nodes = new Node[SIZE]; }

Depth-First Search24 DFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm DFS(G, v) Input graph G and a start vertex v of G Output labeling of the edges of G in the connected component of v as discovery edges and back edges setLabel(v, VISITED) for all e  G.incidentEdges(v) if getLabel(e)  UNEXPLORED w  G.opposite(v,e) if getLabel(w)  UNEXPLORED setLabel(e, DISCOVERY) DFS(G, w) else setLabel(e, BACK) Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u  G.vertices() setLabel(u, UNEXPLORED) for all e  G.edges() setLabel(e, UNEXPLORED) for all v  G.vertices() if getLabel(v)  UNEXPLORED DFS(G, v)

Depth-First Search25 Example DB A C ED B A C ED B A C E discovery edge back edge A visited vertex A unexplored vertex unexplored edge

Depth-First Search26 Example (cont.) DB A C E DB A C E DB A C E D B A C E

Depth-First Search27 DFS and Maze Traversal The DFS algorithm is similar to a classic strategy for exploring a maze –We mark each intersection, corner and dead end (vertex) visited –We mark each corridor (edge ) traversed –We keep track of the path back to the entrance (start vertex) by means of a rope (recursion stack)

Depth-First Search28 Properties of DFS Property 1 DFS(G, v) visits all the vertices and edges in the connected component of v Property 2 The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v DB A C E

Depth-First Search29 Complexity of DFS DFS runs in O(n  m) time provided the graph is represented by the adjacency list structure

30 Biconnectivity (This material replaces that in section 6.3.2) SEAPVD MIA SNA ORD FCO We worry about points of failure in the graph.

31 Separation Edges and Vertices Definitions –Let G be a connected graph –A separation edge of G is an edge whose removal disconnects G –A separation vertex of G is a vertex whose removal disconnects G –A graph is bi-connected if for any two vertices of the graph there are two disjoint paths between them (or a simple path with joins them) Applications –Separation edges and vertices represent single points of failure in a network and are critical to the operation of the network Example –DFW, LGA and LAX are separation vertices –(DFW,LAX) is a separation edge ORDPVD MIA DFW SFO LAX LGA HNL

Finding Articulation points 1. Do a depth first search, numbering the nodes as they are visited in preorder. Call the numbers num(v). If a node has already been visited, we consider the edge to it as a “back edge”. The undirected edges become “directed” by the order they are visited. 2. The root is an articulation point if it has two children. Its removal would create two subtrees. 3. For each node, compute the lowest vertex that can be reached by zero or more tree edges followed by possibly one back edge. Low(v) is the minimum of 1. Num(v) [taking no edges] 2. the lowest back edge among all back edges (v,w) [no tree edges] 3. The lowest Low(w) among all tree edges (v,w)[some tree edges and a back edge] 32

To compute Low, we need a postorder traversal. Given Low, we find articulation points as: 1. The root is an articulation point if it has more than one child 2. Any other vertex v is an articulation point if and only if v has some child (w) in the tree such that Low(w) >=Num(v) 33

In the example below, notice C is an articulation point as C has a child G and Low(G) >=Num(C). D is an articulation point as Low(E) >=Num(D). 34

So how do we compute Low? 35

What is the complexity? Assign DFS Numbers Assign Low Examine nodes for articulation point If we don’t know which is greater n (the number of nodes) or m (the number of edges), we show it as O(n+m) 36

37 Breadth-First Search By levels, typically using queues

38 BFS Facts There are discovery (tree) and cross edges (why no back edges?) – Once marked, don’t follow again. Tree edges form spanning tree Tree edges are paths, minimal in length Cross edges differ by at most one in level Try writing the code to do a BFS

39 Thm 6.19: Algorithms based on BFS Test for connectivity compute spanning forest compute connected components find shortest path between two points (in number of links) compute a cycle in graph, or report none (have cross edges) Good for shortest path information, while DFS better for complex connectivity questions

40 Digraphs A digraph is a graph whose edges are all directed –Short for “directed graph” Applications –one-way streets –flights –task scheduling Fundamental issue is reachability A C E B D

Complexity For each node in a digraph, how would you see which other nodes are reachable from that node? What would the complexity be? 41

42 Digraph Properties A graph G=(V,E) such that –Each edge goes in one direction: Edge (a,b) goes from a to b, but not b to a. If G is simple, m < n*(n-1) – at most an edge between each node and every other node. If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of in-edges and out-edges in time proportional to their size. A C E B D

43 Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started The good life ics141 ics131 ics121 ics53 ics52 ics51 ics23ics22ics21 ics161 ics151 ics171

44 Digraph Facts Directed DFS gives directed paths from root to each reachable vertex Used for O(n(n+m)) algorithm [dfs is O(n+m), these algorithms use n dfs searches] –Find all induced subgraphs (from each vertex, v, find subgraph reachable from v) –Test for strong connectivity –Compute the transitive closure Directed BFS has discovery, back, cross edges

45 Directed DFS We can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their direction In the directed DFS algorithm, we have four types of edges –discovery edges –back edges (to ancestor) –forward edges (to descendant) –cross edges (to other) A directed DFS starting at avertex s determines the vertices reachable from s A C E B D

46 Reachability DFS tree rooted at v: vertices reachable from v via directed paths A C E B D F A C ED A C E B D F

47 Strong Connectivity Each vertex can reach all other vertices a d c b e f g

48 Pick a vertex v in G. Perform a DFS from v in G. –If there’s a w not visited, return not strongly connected Let G’ be G with edges reversed. Perform a DFS from v in G’. –If there’s a w not visited, return not strongly connected –Else, return strongly connected Running time: O(n+m). Strong Connectivity Algorithm G: G’: a d c b e f g a d c b e f g

49 Maximal subgraphs such that each vertex can reach all other vertices in the subgraph Can also be done in O(n+m) time using DFS, but is more complicated (similar to biconnectivity). Strongly Connected Components { a, c, g } { f, d, e, b } a d c b e f g

50 Transitive Closure Given a digraph G, the transitive closure of G is the digraph G* such that –G* has the same vertices as G –if G has a directed path from u to v (u  v), G* has a directed edge from u to v The transitive closure provides reachability information about a digraph B A D C E B A D C E G G*

51 Computing the Transitive Closure We can perform DFS starting at each vertex –O(n(n+m)) If there's a way to get from A to B and from B to C, then there's a way to get from A to C. Alternatively... Use dynamic programming: The Floyd-Warshall Algorithm

52 Floyd-Warshall Transitive Closure Idea #1: Number the vertices 1, 2, …, n. Idea #2: Consider paths that use only vertices numbered 1, 2, …, k, as intermediate vertices: k j i Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k-1 Uses only vertices numbered 1,…,k (add this edge if it’s not already in)

53 Floyd-Warshall’s Algorithm Floyd-Warshall’s algorithm numbers the vertices of G as v 1, …, v n and computes a series of digraphs G 0, …, G n –G 0 =G –G k has directed edge (v i, v j ) if G has directed path from v i to v j with intermediate vertices in the set {v 1, …, v k } We have that G n = G* In phase k, digraph G k is computed from G k  1 Running time: O(n 3 ), assuming areAdjacent is O(1) (e.g., adjacency matrix) Algorithm FloydWarshall(G) Input digraph G (vertices numbered) Output transitive closure G* of G G 0  G for k  1 to n do G k  G k  1 for i  1 to n (i  k) do for j  1 to n (j  i, k) do if G k  1.areAdjacent(v i v k )  G k  1.areAdjacent(v k v j ) if  G k.areAdjacent(v i v j ) G k.insertDirectedEdge(v i v j ) return G n

54 Floyd-Warshall Example JFK BOS MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6

y 2 3yyy 4y 5yy 6yy 7yy

56 Floyd-Warshall, Conclusion JFK MIA ORD LAX DFW SFO v 2 v 1 v 3 v 4 v 5 v 6 BOS

Recursion What if you didn’t want to use recursion to do a DFS? You could turn the parent edge around to tell you how to get back. This is termed doing a DFS “in-place” Obviously the edge would have to be flipped again after the call 57

58 DAGs and Topological Ordering A directed acyclic graph (DAG) is a digraph that has no directed cycles A topological ordering of a digraph is a numbering v 1, …, v n of the vertices such that for every edge (v i, v j ), we have i  j Example: in a task scheduling digraph, a topological ordering a task sequence that satisfies the precedence constraints Theorem A digraph admits a topological ordering if and only if it is a DAG B A D C E DAG G B A D C E Topological ordering of G v1v1 v2v2 v3v3 v4v4 v5v5

59 write c.s. program play Topological Sorting Number vertices, so that (u,v) in E implies u < v wake up eat nap study computer sci. more c.s. work out sleep dream about graphs A typical student day make cookies for professors

60 For each vertex compute its indegree Keep a set S of all vertices with indegree 0 num = 0; While S is not empty { u = s.pop() u.number = ++num; for each of u’s successors w w.indegree— if (w.indegree ==0) S.add(w) } if (num <nodeCt) graph has a directed cycle (text differs) Algorithm for Topological Sorting

61 Topological Sorting DFS Algorithm Simulate the algorithm by using depth-first search O(n+m) time. Algorithm topologicalDFS(G, v) Input graph G and a start vertex v of G (having no input arcs from unvisited nodes) Output labeling of the vertices of G in the connected component of v setLabel(v,VISITED) for all edges (v,w) if getLabel(w)  !VISITED topologicalDFS(G, w) Label v with topological number n n  n - 1 Algorithm topologicalDFS(G) Input dag G Output topological ordering of G n  G.numVertices() for all u  G.vertices() setLabel(u, !VISITED) for all v  G.vertices() which have no incoming edges. if getLabel(v)  VISITED topologicalDFS(G, v)

62 Topological Sorting Example a f c g e i d b h

63 Topological Sorting Example d 3 9

64 Topological Sorting Example