Graphs 337 LAX Graphs 3 4 7 1

  • Slides: 42
Download presentation
Graphs 337 LAX Graphs 3 4 7 1 1233 ORD 802 SFO 1843 DFW

Graphs 337 LAX Graphs 3 4 7 1 1233 ORD 802 SFO 1843 DFW 1

Outline and Reading Graphs (§ 6. 1) n n n Definition Applications Terminology Properties

Outline and Reading Graphs (§ 6. 1) n n n Definition Applications Terminology Properties ADT Data structures for graphs (§ 6. 2) n n n Edge list structure Adjacency matrix structure Graphs 2

Graph A graph is a pair (V, E), where n n n V is

Graph A graph is a pair (V, E), where n n n 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: n 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 SFO 337 HNL 2555 LAX 1843 3 4 17 1233 849 ORD 802 n DFW Graphs 2 PVD 14 7 8 3 1 LGA 1120 10 99 MIA 3

Edge Types Directed edge n n ordered pair of vertices (u, v) first vertex

Edge Types Directed edge n n ordered pair of vertices (u, v) first vertex u is the origin second vertex v is the destination e. g. , a flight ORD flight AA 1206 PVD ORD 849 miles PVD Undirected edge n n unordered pair of vertices (u, v) e. g. , a flight route Directed graph n n all the edges are directed e. g. , route network Undirected graph n n all the edges are undirected e. g. , flight network Graphs 4

Applications Electronic circuits n n Printed circuit board Integrated circuit Transportation networks n n

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

Terminology End vertices (or endpoints) of an edge n U and V are the

Terminology End vertices (or endpoints) of an edge n U and V are the endpoints of a a Edges incident on a vertex n a, d, and b are incident on V Adjacent vertices n Parallel edges n j Z i g f h and i are parallel edges Self-loop n e W X has degree 5 h X c Degree of a vertex n b d U U and V are adjacent V Y j is a self-loop Graphs 6

Terminology (cont. ) Path n n sequence of alternating vertices and edges begins with

Terminology (cont. ) Path n n 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 n path such that all its vertices and edges are distinct Examples n n 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 Graphs a U c V b d P 2 P 1 X e W h Z g f Y 7

Terminology (cont. ) Cycle n n circular sequence of alternating vertices and edges each

Terminology (cont. ) Cycle n n circular sequence of alternating vertices and edges each edge is preceded and followed by its endpoints Simple cycle n cycle such that all its vertices and edges are distinct Examples n n 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 Graphs a U c V b d C 2 X e C 1 g W f h Z Y 8

Properties Property 1 Notation Sv deg(v) = 2 m n m deg(v) Proof: each

Properties Property 1 Notation Sv deg(v) = 2 m n m deg(v) Proof: each edge is counted twice Property 2 number of vertices number of edges degree of vertex v Example n n = 4 n m = 6 n deg(v) = 3 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? Graphs 9

Main Methods of the Graph ADT Vertices and edges n n Update methods are

Main Methods of the Graph ADT Vertices and edges n n Update methods are positions store elements n n Accessor methods n n n n n a. Vertex() incident. Edges(v) end. Vertices(e) is. Directed(e) origin(e) destination(e) opposite(v, e) are. Adjacent(v, w) n n insert. Vertex(o) insert. Edge(v, w, o) insert. Directed. Edge(v, w, o) remove. Vertex(v) remove. Edge(e) Generic methods n n Graphs num. Vertices() num. Edges() vertices() edges() 10

Implementation Use common sense But there is one “representation” that you should see… Graphs

Implementation Use common sense But there is one “representation” that you should see… Graphs 11

Adjacency Matrix Structure a Edge list structure Augmented vertex objects n n b u

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

Depth-First Search A B D E C Graphs 13

Depth-First Search A B D E C Graphs 13

Outline and Reading Definitions (§ 6. 1) n n n Subgraph Connectivity Spanning trees

Outline and Reading Definitions (§ 6. 1) n n n Subgraph Connectivity Spanning trees and forests Depth-first search (§ 6. 3. 1) n n Algorithm Example Properties Analysis Applications of DFS (§ 6. 5) n n Path finding Cycle finding Graphs 14

Subgraphs A subgraph S of a graph G is a graph such that n

Subgraphs A subgraph S of a graph G is a graph such that n n The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges of G Subgraph A spanning subgraph of G is a subgraph that contains all the vertices of G Spanning subgraph Graphs 15

Connectivity A graph is connected if there is a path between every pair of

Connectivity A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G Connected graph Non connected graph with two connected components Graphs 16

Trees and Forests A (free) tree is an undirected graph T such that T

Trees and Forests A (free) tree is an undirected graph T such that T is connected n T has no cycles This definition of tree is different from the one of a rooted tree n A forest is an undirected graph without cycles The connected components of a forest are trees Graphs Tree Forest 17

Spanning Trees and Forests A spanning tree of a connected graph is a spanning

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 A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree Graphs 18

Depth-First Search Depth-first search (DFS) is a general technique for traversing a graph A

Depth-First Search Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G n n 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 Graphs 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 n n Find and report a path between two given vertices Find a cycle in the graph 19

DFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of vertices

DFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u G. vertices() set. Label(u, UNEXPLORED) for all e G. edges() set. Label(e, UNEXPLORED) for all v G. vertices() if get. Label(v) = UNEXPLORED DFS(G, v) 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 set. Label(v, VISITED) for all e G. incident. Edges(v) if get. Label(e) = UNEXPLORED w opposite(v, e) if get. Label(w) = UNEXPLORED set. Label(e, DISCOVERY) DFS(G, w) else set. Label(e, BACK) Graphs 20

Example unexplored vertex visited vertex unexplored edge discovery edge back edge A A A

Example unexplored vertex visited vertex unexplored edge discovery edge back edge A A A B E D E C A B D A D E B C C Graphs 21

Example (cont. ) A B A D E B C C A A B

Example (cont. ) A B A D E B C C A A B D E B C D E C Graphs 22

DFS and Maze Traversal The DFS algorithm is similar to a classic strategy for

DFS and Maze Traversal The DFS algorithm is similar to a classic strategy for exploring a maze n n n 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) Graphs 23

Properties of DFS Property 1 DFS(G, v) visits all the vertices and edges in

Properties of DFS Property 1 DFS(G, v) visits all the vertices and edges in the connected component of v A Property 2 The discovery edges labeled by DFS(G, v) form a spanning tree of the connected component of v B Graphs D E C 24

Proof of Property 1 Suppose (for contradiction) that at least one vertex s in

Proof of Property 1 Suppose (for contradiction) that at least one vertex s in v’s connected component is not visited. There is some path from v to s in G. Let w be first unvisited vertex on this path (w may be s) Since w is first unvisited vertex, it has a neighbor u that was visited. Graphs 25

Proof of Property 1 But when visiting u, we must have considered edge (u,

Proof of Property 1 But when visiting u, we must have considered edge (u, w), thus it cannot be correct that w is unvisited. Thus, there can be no unvisited vertices in the connected component of v. Graphs 26

Proof of Property 2 Note that we only mark edges as discovery when we

Proof of Property 2 Note that we only mark edges as discovery when we go to unvisited vertices. Thus we can never form a cycle with discovery edges. I. e. the discovery edges form a tree. This is a spanning tree because we visit each tree in the connected component of v Graphs 27

Analysis of DFS Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled

Analysis of DFS Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice n n once as UNEXPLORED once as VISITED Each edge is labeled twice n n once as UNEXPLORED once as DISCOVERY or BACK Method incident. Edges is called once for each vertex DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure n Recall that Sv deg(v) = 2 m Graphs 28

Path Finding We can specialize the DFS algorithm to find a path between two

Path Finding We can specialize the DFS algorithm to find a path between two given vertices u and z 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 path. DFS(G, v, z) set. Label(v, VISITED) S. push(v) if v = z return S. elements() for all e G. incident. Edges(v) if get. Label(e) = UNEXPLORED w opposite(v, e) if get. Label(w) = UNEXPLORED set. Label(e, DISCOVERY) S. push(e) path. DFS(G, w, z) S. pop(e) else set. Label(e, BACK) S. pop(v) Graphs 29

Cycle Finding We can specialize the DFS algorithm to find a simple cycle We

Cycle Finding We can specialize the DFS algorithm to find a simple cycle 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 Algorithm cycle. DFS(G, v, z) set. Label(v, VISITED) S. push(v) for all e G. incident. Edges(v) if get. Label(e) = UNEXPLORED w opposite(v, e) S. push(e) if get. Label(w) = UNEXPLORED set. Label(e, DISCOVERY) path. DFS(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) Graphs 30

Breadth-First Search L 0 L 1 A B L 2 Graphs C E D

Breadth-First Search L 0 L 1 A B L 2 Graphs C E D F 31

Outline and Reading Breadth-first search (§ 6. 3. 3) n n n Algorithm Example

Outline and Reading Breadth-first search (§ 6. 3. 3) n n n Algorithm Example Properties Analysis Applications DFS vs. BFS (§ 6. 3. 3) n n Comparison of applications Comparison of edge labels Graphs 32

Breadth-First Search Breadth-first search (BFS) is a general technique for traversing a graph A

Breadth-First Search Breadth-first search (BFS) is a general technique for traversing a graph A BFS traversal of a graph G n n 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 Graphs 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 n n Find and report a path with the minimum number of edges between two given vertices Find a simple cycle, if there is one 33

BFS Algorithm The algorithm uses a mechanism for setting and getting “labels” of vertices

BFS Algorithm 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() set. Label(u, UNEXPLORED) for all e G. edges() set. Label(e, UNEXPLORED) for all v G. vertices() if get. Label(v) = UNEXPLORED BFS(G, v) Algorithm BFS(G, s) L 0 new empty sequence L 0. insert. Last(s) set. Label(s, VISITED) i 0 while Li. is. Empty() Li +1 new empty sequence for all v Li. elements() for all e G. incident. Edges(v) if get. Label(e) = UNEXPLORED w opposite(v, e) if get. Label(w) = UNEXPLORED set. Label(e, DISCOVERY) set. Label(w, VISITED) Li +1. insert. Last(w) else set. Label(e, CROSS) i i +1 Graphs 34

Example unexplored vertex visited vertex unexplored edge discovery edge cross edge A A L

Example unexplored vertex visited vertex unexplored edge discovery edge cross edge A A L 0 L 1 L 0 C E B L 1 D F C E A B A F A B C E Graphs D D F 35

Example (cont. ) L 0 L 1 L 0 A B C E L

Example (cont. ) L 0 L 1 L 0 A B C E L 0 L 1 D F L 0 C E B L 2 A B L 2 L 1 D Graphs C E D F A B L 2 F A C E D F 36

Example (cont. ) L 0 L 1 A B L 2 C E L

Example (cont. ) L 0 L 1 A B L 2 C E L 1 D B L 2 F A C E D F A B L 2 L 0 C E D F Graphs 37

Properties Notation A Gs: connected component of s Property 1 BFS(G, s) visits all

Properties Notation A Gs: connected component of s Property 1 BFS(G, s) visits all the vertices and edges of Gs B E Property 2 The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs Property 3 n n L 0 L 1 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 (i. e. the path above is the shortest possible) Graphs C F A B L 2 D C E D F 38

Analysis Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice n

Analysis Setting/getting a vertex/edge label takes O(1) time Each vertex is labeled twice n n once as UNEXPLORED once as VISITED Each edge is labeled twice n n once as UNEXPLORED once as DISCOVERY or CROSS Each vertex is inserted once into a sequence Li Method incident. Edges is called once for each vertex BFS runs in O(n + m) time provided the graph is represented by the adjacency list structure n Recall that Sv deg(v) = 2 m Graphs 39

Applications We can specialize the BFS traversal of a graph G to solve the

Applications We can specialize the BFS traversal of a graph G to solve the following problems in O(n + m) time n n 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 Graphs 40

DFS vs. BFS Applications DFS BFS Spanning forest, connected components, paths, cycles Shortest paths

DFS vs. BFS Applications DFS BFS Spanning forest, connected components, paths, cycles Shortest paths Biconnected components L 0 A B C E D L 1 B L 2 F DFS A C E D F BFS Graphs 41

DFS vs. BFS (cont. ) Back edge (v, w) n Cross edge (v, w)

DFS vs. BFS (cont. ) Back edge (v, w) n Cross edge (v, w) w is an ancestor of v in the tree of discovery edges n L 0 A B w is in the same level as v or in the next level in the tree of discovery edges C E D L 1 B L 2 F DFS A C E D F BFS Graphs 42