Graphs Part 1 ORD SFO LAX DFW 1843 802 1743 337 1233 2/24/2019 2:01 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Graphs Part 1 1843 ORD SFO 802 1743 337 LAX 1233 DFW Graphs
Graphs PVD ORD SFO LGA HNL LAX DFW MIA A graph is a pair (V, E), where V is a set of nodes, called vertices E is a collection of pairs of vertices, called edges Vertices and edges are positions 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 849 PVD 1843 ORD 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 LAX 1233 DFW 1120 MIA Graphs
Edge Types flight AA 1206 ORD PVD weight 849 miles ORD PVD Directed edge ordered pair of vertices (u,v) first vertex u is the 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 all the edges are directed e.g., route network Undirected graph all the edges are undirected e.g., flight network flight AA 1206 ORD PVD weight 849 miles ORD PVD Graphs
Applications Electronic circuits Transportation networks Printed circuit board Integrated circuit Transportation networks Highway network Flight network Computer networks Local area network Internet Web Databases Entity-relationship diagram Graphs
Terminology X U V W Z Y a c b e d f g h i j 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 U and V are adjacent Degree of a vertex X has degree 5 Parallel edges h and i are parallel edges Self-loop j is a self-loop X U V W Z Y a c b e d f g h i j Graphs
Terminology (cont.) V a b P1 d U X Z P2 h c e W g f Y Path Simple path sequence of alternating vertices and edges begins with a vertex ends with a vertex each edge is preceded and followed by its endpoints Simple path path such that all its vertices and edges are distinct Examples P1=(V,b,X,h,Z) is a simple path P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple V a b P1 d U X Z P2 h c e W g f Y Graphs
Terminology (cont.) V a b d U X Z C2 h e C1 c W g f Y Cycle circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints Simple cycle cycle such that all its vertices and edges are distinct Examples C1=(V,b,X,g,Y,f,W,c,U,a,) is a simple cycle C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple V a b d U X Z C2 h e C1 c W g f Y Graphs
Exercise on Terminology Graphs 二○一九年二月二十四日 Exercise on Terminology Number of vertices? Number of edges? What type of the graph is it? Show the end vertices of the edge with largest weight Show the vertices of smallest degree and largest degree Show the edges incident to the vertices in the above question Identify the shortest simple path from HNL to PVD Identify the simple cycle with the most edges ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 Represent connectivity information between objects
Exercise Properties of Undirected Graphs Property 1 – Total degree Σ 𝑣 𝑑𝑒𝑔 𝑣 = ?? Property 2 – Total number of edges In an undirected graph with no self-loops and no multiple edges 𝑚≤ ? ? 0≤𝑚 Each vertex can have degree at most ? ? Notation 𝑛 number of vertices 𝑚 number of edges deg 𝑣 degree of vertex v Example 𝑛=4 𝑚=6 deg 𝑣 =3 A graph with given number of vertices (4) and maximum number of edges
Properties of Undirected Graphs Property 1 – Total degree Σ 𝑣 𝑑𝑒𝑔 𝑣 =2𝑚 Property 2 – Total number of edges In an undirected graph with no self-loops and no multiple edges 𝑚≤ 𝑛(𝑛−1) 2 0≤𝑚 Proof: Each vertex can have degree at most 𝑛−1 Notation 𝑛 number of vertices 𝑚 number of edges deg(𝑣) degree of vertex v Example 𝑛=4 𝑚=6 deg 𝑣 =3 A graph with given number of vertices (4) and maximum number of edges
Directed Graphs (Digraphs) A digraph is a graph whose edges are all directed Short for “directed graph” Applications one-way streets flights task scheduling A C E B D Directed Graphs
Digraph Application Scheduling: edge (a,b) means task a must be completed before b can be started The good life cs141 cs131 cs121 cs53 cs52 cs51 cs46 cs22 cs21 cs161 cs151 cs171 Directed Graphs
Exercise Properties of Directed Graphs 2019/2/24 Exercise Properties of Directed Graphs Notation 𝑛 number of vertices 𝑚 number of edges deg(𝑣) degree of vertex v Property 1 – Total in-degree and out-degree Σ 𝑣 𝑖𝑛 deg(𝑣)=? Σ 𝑣 𝑜𝑢𝑡 deg 𝑣 =? Property 2 – Total number of edges In an directed graph with no self-loops and no multiple edges 𝑚≤𝑈𝑝𝑝𝑒𝑟𝐵𝑜𝑢𝑛𝑑? 𝐿𝑜𝑤𝑒𝑟𝐵𝑜𝑢𝑛𝑑?≤𝑚 Example 𝑛=? 𝑚=? deg 𝑣 =? A graph with given number of vertices (4) and maximum number of edges
Properties of Directed Graphs 2019/2/24 Properties of Directed Graphs Property 1 – Total in-degree and out-degree Σ 𝑣 𝑖𝑛 deg(𝑣)=𝑚 Σ 𝑣 𝑜𝑢𝑡 deg 𝑣 =𝑚 Property 2 – Total number of edges In an directed graph with no self-loops and no multiple edges 𝑚≤𝑛 𝑛−1 0≤𝑚 Notation 𝑛 number of vertices 𝑚 number of edges deg(𝑣) degree of vertex v Example 𝑛=4 𝑚=12 deg 𝑣 =6 A graph with given number of vertices (4) and maximum number of edges
Digraph Properties A graph G=(V,E) such that B D 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) If we keep in-edges and out-edges in separate adjacency lists, we can perform listing of incoming edges and outgoing edges in time proportional to their size Directed Graphs
Terminology Connectivity 𝑢 𝑣 Connected graph 𝑢 and 𝑣 are reachable Given two vertices 𝑢 and 𝑣, we say 𝑢 reaches 𝑣, and that 𝑣 is reachable from 𝑢, if there exists a path from 𝑢 to 𝑣. In an undirected graph reachability is symmetric A graph is connected if there is a path between every pair of vertices A digraph is strongly connected if for any two vertices u and v of G, u reaches v and v reaches u 𝑢 𝑣 Connected digraph 𝑢 and 𝑣 are not mutually reachable
Terminology Subgraphs A subgraph 𝐻 of a graph 𝐺 is a graph whose vertices and edges are subsets of 𝐺 A spanning subgraph of 𝐺 is a subgraph that contains all the vertices of 𝐺 A connected component of a graph 𝐺 is a maximal connected subgraph of 𝐺 Spanning subgraph Non connected graph with two connected components
Terminology Trees and Forests A forest is a graph without cycles A (free) tree is connected forest This definition of tree is different from the one of a rooted tree The connected components of a forest are trees
Spanning Trees and Forests A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks Graph Spanning tree
Vertices and Edges Modeling Graphs A graph is a collection of vertices and edges. We model the abstraction as a combination of three data types: Vertex, Edge, and Graph. A Vertex is a lightweight object that stores an arbitrary element provided by the user (e.g., an airport code) We assume it supports a method, element(), to retrieve the stored element. An Edge stores an associated object (e.g., a flight number, travel distance, cost), retrieved with the element( ) method. Graphs
Modeling Graphs Graph ADT Graphs
Exercise on ADT insertVertex(𝑖𝑎ℎ) outgoingEdges(𝑜𝑟𝑑) Graphs 二○一九年二月二十四日 Exercise on ADT outgoingEdges(𝑜𝑟𝑑) incomingEdges(𝑜𝑟𝑑) outDegree(𝑜𝑟𝑑) endVertices( 𝑙𝑔𝑎, 𝑚𝑖𝑎 ) opposite(𝑑𝑓𝑤, 𝑑𝑓𝑤, 𝑙𝑔𝑎 ) insertVertex(𝑖𝑎ℎ) insertEdge(𝑚𝑖𝑎, 𝑝𝑣𝑑, 120) removeVertex(𝑜𝑟𝑑) removeEdge( 𝑑𝑓𝑤, 𝑜𝑟𝑑 ) ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142 Represent connectivity information between objects
Edge List Structure Vertex object Edge object Vertex sequence element reference to position in vertex sequence Edge object origin vertex object destination vertex object reference to position in edge sequence Vertex sequence sequence of vertex objects Edge sequence sequence of edge objects Graphs
Adjacency List Structure Incidence sequence for each vertex sequence of references to edge objects of incident edges Augmented edge objects references to associated positions in incidence sequences of end vertices Graphs
Adjacency Matrix Structure Edge list structure Augmented vertex objects Integer key (index) associated with vertex 2D-array 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 Graphs
Also there is the “Adjacency Map Structure” Graphs 2/24/2019 2:01 PM Performance n vertices, m edges no parallel edges no self-loops Edge List Adjacency List Adjacency Matrix Space n + m n2 incidentEdges(v) m deg(v) n areAdjacent (v, w) min(deg(v), deg(w)) 1 insertVertex(o) insertEdge(v, w, o) removeVertex(v) removeEdge(e) Also there is the “Adjacency Map Structure” Also there is the “Adjacency Map Structure” Graphs
Depth-First Search 2/24/2019 2:01 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Depth-First Search D B A C E Depth-First Search
Depth-First Search Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G Visits all the vertices and edges of G Determines whether G is connected Computes the connected components of G Computes a spanning forest of G DFS on a graph with n vertices and m edges takes O(n + m ) time DFS can be further extended to solve other graph problems Find and report a path between two given vertices Find a cycle in the graph Depth-first search is to graphs what Euler tour is to binary trees Depth-First Search
DFS Algorithm from a Vertex Depth-First Search
Java Implementation Depth-First Search
Example unexplored vertex visited vertex unexplored edge B A C E A A visited vertex unexplored edge discovery edge back edge D B A C E D B A C E Depth-First Search
Example (cont.) D B A C E D B A C E D B A C E D B A C E Depth-First Search
Exercise DFS Algorithm Perform DFS of the following graph, start from vertex A Assume adjacent edges are processed in alphabetical order Number vertices in the order they are visited Label back edges C B A E D F
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 Search
Properties of DFS Property 1 Property 2 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 D B A C E Depth-First Search
Analysis of DFS Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice once as UNEXPLORED once as VISITED Each edge is labeled twice once as DISCOVERY or BACK Method incidentEdges is called once for each vertex DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure Recall that Sv deg(v) = 2m Depth-First Search
Path Finding We can specialize the DFS algorithm to find a path between two given vertices u and z using the template method pattern We call DFS(G, u) with u as the start vertex We use a stack S to keep track of the path between the start vertex and the current vertex As soon as destination vertex z is encountered, we return the path as the contents of the stack Algorithm pathDFS(G, v, z) setLabel(v, VISITED) S.push(v) if v = z return S.elements() for all e G.incidentEdges(v) if getLabel(e) = UNEXPLORED w opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) S.push(e) pathDFS(G, w, z) S.pop(e) else setLabel(e, BACK) S.pop(v) Depth-First Search
Path Finding in Java Depth-First Search
Cycle Finding Algorithm cycleDFS(G, v, z) setLabel(v, VISITED) S.push(v) for all e G.incidentEdges(v) if getLabel(e) = UNEXPLORED w opposite(v,e) S.push(e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) pathDFS(G, w, z) S.pop(e) else T new empty stack repeat o S.pop() T.push(o) until o = w return T.elements() S.pop(v) We can specialize the DFS algorithm to find a simple cycle using the template method pattern We use a stack S to keep track of the path between the start vertex and the current vertex As soon as a back edge (v, w) is encountered, we return the cycle as the portion of the stack from the top to vertex w Depth-First Search
DFS for an Entire Graph 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 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 Search
Breadth-First Search L0 L1 L2 2/24/2019 2:01 PM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Breadth-First Search C B A E D L0 L1 F L2 Breadth-First Search
Breadth-First Search Breadth-first search (BFS) is a general technique for traversing a graph A BFS traversal of a graph G Visits all the vertices and edges of G Determines whether G is connected Computes the connected components of G Computes a spanning forest of G BFS on a graph with n vertices and m edges takes O(n + m ) time BFS can be further extended to solve other graph problems Find and report a path with the minimum number of edges between two given vertices Find a simple cycle, if there is one Breadth-First Search
BFS Algorithm Algorithm BFS(G, s) L0 new empty sequence L0.addLast(s) setLabel(s, VISITED) i 0 while Li.isEmpty() Li +1 new empty sequence for all v Li.elements() for all e G.incidentEdges(v) if getLabel(e) = UNEXPLORED w opposite(v,e) if getLabel(w) = UNEXPLORED setLabel(e, DISCOVERY) setLabel(w, VISITED) Li +1.addLast(w) else setLabel(e, CROSS) i i +1 The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G 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 BFS(G, v) Breadth-First Search
Java Implementation Breadth-First Search
Example unexplored vertex visited vertex unexplored edge C B A E D L0 L1 F A unexplored vertex A visited vertex unexplored edge discovery edge cross edge L0 L0 A A L1 L1 B C D B C D E F E F Breadth-First Search
Example (cont.) L0 L1 L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F C B A E D Breadth-First Search
Example (cont.) L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F A B C D E F C B Breadth-First Search
Exercise BFS Algorithm Perform DFS of the following graph, start from vertex A Assume adjacent edges are processed in alphabetical order Number vertices in the order they are visited and note the level they are in Label back edges C B A E D F
Properties of BFS Notation Property 1 Property 2 Property 3 Gs: connected component of s Property 1 BFS(G, s) visits all the vertices and edges of Gs Property 2 The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs Property 3 For each vertex v in Li The path of Ts from s to v has i edges Every path from s to v in Gs has at least i edges A B C D E F L0 A L1 B C D L2 E F Breadth-First Search
Analysis Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice once as UNEXPLORED once as VISITED Each edge is labeled twice once as DISCOVERY or CROSS Each vertex is inserted once into a sequence Li Method incidentEdges is called once for each vertex BFS runs in O(n + m) time provided the graph is represented by the adjacency list structure Recall that Sv deg(v) = 2m Breadth-First Search
Applications Using the template method pattern, we can specialize the BFS traversal of a graph G to solve the following problems in O(n + m) time Compute the connected components of G Compute a spanning forest of G Find a simple cycle in G, or report that G is a forest Given two vertices of G, find a path in G between them with the minimum number of edges, or report that no such path exists Breadth-First Search
DFS vs. BFS Applications DFS BFS DFS BFS Spanning forest, connected components, paths, cycles Shortest paths Biconnected components C B A E D L0 L1 F L2 A B C D E F DFS BFS Breadth-First Search
DFS vs. BFS (cont.) Back edge (v,w) Cross edge (v,w) DFS BFS w is an ancestor of v in the tree of discovery edges Cross edge (v,w) w is in the same level as v or in the next level C B A E D L0 L1 F L2 A B C D E F DFS BFS Breadth-First Search
More About Directed Graphs 2/24/2019 2:01 PM More About Directed Graphs JFK BOS MIA ORD LAX DFW SFO Directed Graphs
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 forward edges cross edges A directed DFS starting at a vertex s determines the vertices reachable from s E D C B A Directed Graphs
Reachability DFS tree rooted at v: vertices reachable from v via directed paths E D E D C A C F E D A B C F A B Directed Graphs
Strong Connectivity Each vertex can reach all other vertices a g c d e b e f g Directed Graphs
Strong Connectivity Algorithm Pick a vertex v in G Perform a DFS from v in G If there’s a w not visited, print “no” Let G’ be G with edges reversed Perform a DFS from v in G’ Else, print “yes” Running time: O(n+m) a G: g c d e b f a G’: g c d e b f Directed Graphs
Strongly Connected Components 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. Google: Kosaraju's Algorithm a d c b e f g { a , c , g } { f , d , e , b } Directed Graphs
Transitive Closure D E 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 G C A D E B C A G* Directed Graphs
Computing the Transitive Closure 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. We can perform DFS starting at each vertex O(n(n+m)) Optional Alternatively ... Use dynamic programming: The Floyd-Warshall Algorithm Directed Graphs
Topological Ordering Directed Graphs Directed Graphs 2/24/2019 2:01 PM JFK BOS MIA ORD LAX DFW SFO Directed Graphs
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 v1 , …, vn of the vertices such that for every edge (vi , vj), 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 C A DAG G v4 v5 D E v2 B v3 C v1 Topological ordering of G A Directed Graphs
Topological Sorting Number vertices, so that (u,v) in E implies u < v 1 A typical student day wake up 2 3 eat study computer sci. 4 5 nap more c.s. 7 play 8 write c.s. program 6 9 work out bake cookies 10 sleep 11 dream about graphs Directed Graphs
Algorithm for Topological Sorting Note: This algorithm is different than the one in the book Running time: O(n + m) Algorithm TopologicalSort(G) H G // Temporary copy of G n G.numVertices() while H is not empty do Let v be a vertex with no outgoing edges Label v n n n - 1 Remove v from H Directed Graphs
Implementation with DFS 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 Output labeling of the vertices of G in the connected component of v setLabel(v, VISITED) for all e G.outEdges(v) { outgoing edges } w opposite(v,e) if getLabel(w) = UNEXPLORED { e is a discovery edge } topologicalDFS(G, w) else { e is a forward or cross edge } 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, UNEXPLORED) for all v G.vertices() if getLabel(v) = UNEXPLORED topologicalDFS(G, v) Directed Graphs
Topological Sorting Example Directed Graphs
Topological Sorting Example 9 Directed Graphs
Topological Sorting Example 8 9 Directed Graphs
Topological Sorting Example 7 8 9 Directed Graphs
Topological Sorting Example 6 7 8 9 Directed Graphs
Topological Sorting Example 6 5 7 8 9 Directed Graphs
Topological Sorting Example 4 6 5 7 8 9 Directed Graphs
Topological Sorting Example 3 4 6 5 7 8 9 Directed Graphs
Topological Sorting Example 2 3 4 6 5 7 8 9 Directed Graphs
Topological Sorting Example 2 1 3 4 6 5 7 8 9 Directed Graphs
Java Implementation Directed Graphs
Reading [G] Chapter 14: Graphs 14.1, 14.2, 14.3, 14.4, and 14.5 Floyd-Warshall Algorithm is now Optional [L] Section 3.5 : Depth-First Search and Breadth-First Search [L] Section 4.2 : Topological Sorting Watch from Course 6 01 Introduction, 02 Breadth-First Search, and 03 Depth-First Search