Graphs Part 1 Outline and Reading Graphs 13






























































- Slides: 62
Graphs Part 1
Outline and Reading • Graphs (§ 13. 1) – – – Definition Applications Terminology Properties ADT • Data structures for graphs (§ 13. 2) – Edge list structure – Adjacency matrix structure
Graph • 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 337 HNL 2555 LAX 3 4 17 1233 849 ORD 802 SFO 1843 2 14 LGA 7 138 DFW PVD 10 1120 99 MIA
Edge & Graph Types • Edge Types – 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 – Weighted edge • Graph Types – Directed graph (Digraph) • all the edges are directed – Undirected graph • all the edges are undirected – Weighted graph • all the edges are weighted
Applications • Electronic circuits – Printed circuit board – Integrated circuit • Transportation networks – Highway network – Flight network • Computer networks – Local area network – Internet • Databases – Entity-relationship diagram
Terminology • End points (or end vertices) of an edge – U and V are the endpoints of a 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 (multiple) edges – h and i are parallel edges • self-loop – j is a self-loop V b d U h X c e W f Z i g Y j
Terminology (cont. ) • outgoing edges of a vertex – h and b are the outgoing edges of X • incoming edges of a vertex – e, g, and i are incoming edges of X • in-degree of a vertex – X has in-degree 3 • out-degree of a vertex – X has out-degree 2 V h b d Z X e W i g f Y j
Terminology (cont. ) • 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 – 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 V a b d U c P 2 P 1 X e W g f Y h Z
Terminology (cont. ) • 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 – 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 V a U c b d X C 2 e C 1 g W f h Y Z
# of vertices? # 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 SFO 337 HNL 2555 LAX 1843 3 4 17 1233 849 ORD 802 1. 2. 3. 4. 5. 6. 7. 8. Exercise on Terminology 2 14 LGA 7 138 DFW PVD 10 1120 99 MIA
Exercise: Properties of Undirected Graphs Property 1 – Total degree Sv deg(v) = ? Property 2 – Total number of edges In an undirected graph with no self-loops and no multiple edges m Upper bound? Notation n m deg(v) number of vertices number of edges degree of vertex v Example n n = ? n m = ? n deg(v) = ? A graph with given number of vertices (4) and maximum number of edges
Exercise: Properties of Undirected Graphs Property 1 – Total degree Sv deg(v) = 2 m Proof: each edge is counted twice Property 2 – Total number of edges Notation n m deg(v) 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) A graph with given number of vertices (4) and maximum number of edges
Exercise: Properties of Directed Graphs Property 1 – Total in-degree and out-degree Sv in-deg(v) = ? Sv out-deg(v) = ? Property 2 – Total number of edges In a directed graph with no self-loops and no multiple edges m Upper bound? Notation n m deg(v) number of vertices number of edges degree of vertex v Example n n = ? n m = ? n deg(v) = ? A graph with given number of vertices (4) and maximum number of edges
Exercise: Properties of Directed Graphs Property 1 – Total in-degree and out-degree Sv in-deg(v) = m Sv out-deg(v) = m Property 2 – Total number of edges In a directed graph with no self-loops and no multiple edges m n (n - 1) Notation n m deg(v) number of vertices number of edges degree of vertex v Example n n = 4 n m = 12 n deg(v) = 6 A graph with given number of vertices (4) and maximum number of edges
Main Methods of the Graph ADT • Vertices and edges – are positions – store elements • Accessor methods – – – incident. Edges(v) adjacent. Vertices(v) degree(v) end. Vertices(e) opposite(v, e) are. Adjacent(v, w) – is. Directed(e) – origin(e) – destination(e) Specific to directed edges • Update methods – – – insert. Vertex(o) insert. Edge(v, w, o) insert. Directed. Edge(v, w, o) remove. Vertex(v) remove. Edge(e) • Generic methods – – num. Vertices() num. Edges() vertices() edges()
Exercise on ADT 7. insert. Vertex(IAH) 8. insert. Edge(MIA, PVD, 1200) 9. remove. Vertex(ORD) 10. remove. Edge((DFW, ORD)) 11. is. Directed((DFW, LGA)) 12. origin ((DFW, LGA)) 13. destination((DFW, LGA))) incident. Edges(ORD) adjacent. Vertices(ORD) degree(ORD) end. Vertices((LGA, MIA)) opposite(DFW, LGA)) are. Adjacent(DFW, SFO) SFO 337 HNL 2555 LAX 1843 3 4 17 1233 849 ORD 802 1. 2. 3. 4. 5. 6. 2 14 LGA 7 138 DFW PVD 10 1120 99 MIA
Edge List Structure Vertex Sequence (ORD, PVD) 849 ORD (ORD, DFW) 802 LGA (LGA, PVD) 142 PVD (LGA, MIA) 1099 DFW • An edge list can be stored in a sequence, a vector, a list or a dictionary such as a hash table 849 ORD 802 Edge List (DFW, MIA) 1120 MIA 138 DFW 2 14 LGA 7 (DFW, LGA) 1387 PVD 10 1120 99 MIA
Exercise: Edge List Structure Construct an edge list structure for the following graph x u a y z v
Asymptotic Performance • Vertices and edges – are positions – store elements • Accessor methods – Accessing vertex sequence • degree(v) O(1) – Accessing edge list • end. Vertices(e) O(1) • opposite(v, e) O(1) Specific to directed edges • is. Directed(e) O(1) • origin(e) O(1) • destination(e) O(1) • Generic methods – – num. Vertices() O(1) num. Edges() O(1) vertices() O(n) edges() O(m) Edge List Weight Vertex Sequence Directed Degree (ORD, PVD) 849 False ORD 2 (ORD, DFW) 802 False LGA 3 (LGA, PVD) 142 False PVD 2 (LGA, MIA) 1099 False DFW 3 (DFW, LGA) 1387 False MIA 2 (DFW, MIA) 1120 False
Asymptotic Performance of Edge List Structure n vertices, m edges no parallel edges no self-loops Bounds are “big-Oh” Edge List Space n+m incident. Edges(v) adjacent. Vertices(v) are. Adjacent (v, w) m insert. Vertex(o) m 1 insert. Edge(v, w, o) 1 remove. Vertex(v) m 1 remove. Edge(e) Edge List Weight Vertex Sequence Directed Degree (ORD, PVD) 849 False ORD 2 (ORD, DFW) 802 False LGA 3 (LGA, PVD) 142 False PVD 2 (LGA, MIA) 1099 False DFW 3 (DFW, LGA) 1387 False MIA 2 (DFW, MIA) 1120 False
Adjacency List Structure 849 802 ORD Adjacency List ORD (ORD, PVD) (ORD, DFW) LGA (LGA, PVD) (LGA, MIA) PVD (PVD, ORD) (PVD, LGA) DFW (DFW, ORD) (DFW, LGA) (MIA, DFW) MIA DFW (LGA, DFW) (DFW, MIA) 7 138 2 14 PVD LGA 1120 10 99 MIA
Exercise: Adjacency List Structure Construct the adjacency list for the following graph x u a y z v
Asymptotic Performance of Adjacency List Structure • n vertices, m edges • no parallel edges • no self-loops • Bounds are “big-Oh” Space incident. Edges(v) adjacent. Vertices(v) are. Adjacent (v, w) insert. Vertex(o) insert. Edge(v, w, o) remove. Vertex(v) remove. Edge(e) Adjacency List n+m deg(v) min(deg(v), deg(w)) 1 1 deg(v)* 1* ORD (ORD, PVD) (ORD, DFW) LGA (LGA, PVD) (LGA, MIA) PVD (PVD, ORD) (PVD, LGA) DFW (DFW, ORD) (DFW, LGA) (MIA, DFW) MIA (LGA, DFW) (DFW, MIA)
Adjacency Matrix Structure 1 2 3 4 0 0 0 1 1 1 2 1 1 0 0 0 3 1 1 0 0 1 4 0 1 0 849 0: ORD 802 0 3: DFW 7 138 1120 2 14 1: LGA 10 9 2: PVD 9 4: MIA
Exercise: Adjacency Matrix Structure Construct the adjacency matrix for the following graph x u a y z v
Asymptotic Performance of Adjacency Matrix Structure • n vertices, m edges • no parallel edges • no self-loops • Bounds are “big-Oh” Space incident. Edges(v) adjacent. Vertices(v) are. Adjacent (v, w) Adjacency Matrix 1 2 3 4 n 2 0 0 0 1 1 0 n 1 0 0 1 1 1 2 1 1 0 0 0 3 1 1 0 0 1 4 0 1 0 insert. Vertex(o) 1 n 2 insert. Edge(v, w, o) 1 remove. Vertex(v) n 2 1 remove. Edge(e) 0
Asymptotic Performance • n vertices, m edges • no parallel edges • no self-loops • Bounds are “big-Oh” Edge List Adjacency Matrix n+m n 2 m deg(v) n insert. Vertex(o) m 1 min(deg(v), deg(w)) 1 1 n 2 insert. Edge(v, w, o) 1 1 1 remove. Vertex(v) m 1 deg(v) 1 n 2 1 Space incident. Edges(v) adjacent. Vertices(v) are. Adjacent (v, w) remove. Edge(e)
Depth-First Search A B D C E
Outline and Reading • Definitions (§ 13. 1) – Subgraph – Connectivity – Spanning trees and forests • Depth-first search (§ 13. 3. 1) – Algorithm – Example – Properties – Analysis • Applications of DFS – Path finding – Cycle finding
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 of G • A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph
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
Trees and Forests • A (free) tree is an undirected graph T such that – T is connected – T has no cycles This definition of tree is different from the one of a rooted tree • A forest is an undirected graph without cycles • The connected components of a forest are trees Tree Forest
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
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
Example unexplored vertex visited vertex unexplored edge discovery edge back edge A A A B F E G C A D C D E B G F D C E G
Example (cont. ) A B F A D C E B G F A B F E G C A D C D E B G F D C E G
Example (cont. ) A A B D F C A B F D C E G E B G F A(G) = Φ D C E G
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)
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)
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 edges as discovery or back edges A B C E D F
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 v 1 A B F D C E G v 2
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 UNEXPLORED • once as DISCOVERY or BACK A B F D C • Function DFS(G, v) and the method incident. Edges are called once for each vertex E G
Analysis of DFS • DFS runs in O(n + m) time provided the graph is represented by the adjacency list structure – Recall that ∑v deg(v) = 2 m Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for all u G. vertices() O(n) set. Label(u, UNEXPLORED) for all e G. edges() O(m) set. Label(e, UNEXPLORED) for all v G. vertices() O(n +m) 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)
Path Finding • We can specialize the DFS algorithm to find a path between two given vertices v and z using the template method pattern • We call DFS(G, v) with v 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() else set. Label(e, BACK) S. pop()
Breadth-First Search L 0 L 1 A B L 2 C E D F
Outline and Reading • Breadth-first search (Sect. 13. 3. 5) • • • Algorithm Example Properties Analysis Applications • DFS vs. BFS • Comparison of applications • Comparison of edge labels
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
Example L 0 unexplored vertex visited vertex unexplored edge discovery edge cross edge A A L 0 L 1 L 0 C E B D F C E A B A L 1 D F A B C E D F
A A Example (cont. ) L 0 L 1 L 0 A B C E L 0 L 1 F L 0 C E D F L 1 C E D F A B L 2 unexplored edge discovery edge cross edge A B L 2 D L 1 unexplored vertex visited vertex C E D F
A A Example (cont. ) L 0 L 1 A B L 2 C E D F L 1 unexplored edge discovery edge cross edge A B L 2 L 0 unexplored vertex visited vertex C E D F
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
Exercise: BFS Algorithm • Perform BFS 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 edges as discovery or cross edges E D C F B A
Properties Notation A Gs: connected component of s Property 1 B BFS(G, s) visits all the vertices and edges of Gs E 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 C L 1 – The path of Ts from s to v has i edges – Every path from s to v in Gs has at least i edges L 0 F A B L 2 D C E D F
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 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 • Recall that Sv deg(v) = 2 m
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
DFS vs. BFS Applications DFS BFS Spanning forest, connected components, paths, cycles Shortest paths L 0 A B C E DFS D F L 1 A B L 2 C E BFS D F
Cycle Finding • 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 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() else T new empty stack repeat o S. pop() T. push(o) until o = w return T. elements() S. pop()
DFS vs. BFS (cont. ) Cross edge (v, w) Back edge (v, w) – w is in the same level as v or in the next level in the tree of discovery edges – w is an ancestor of v in the tree of discovery edges L 0 A B C E DFS D F L 1 A B L 2 C E BFS D F
Adjacency Matrix Structure a • Edge list structure • Augmented vertex objects v b u w • Integer key (index) associated with vertex • 2 D-array adjacency array • Reference to edge object for adjacent vertices • Null for nonadjacent vertices • The “old fashioned” version just has 0 for no edge and 1 for edge 0 u 1 0 0 2 1 w 2 1 a 2 v b
Edge List Structure • Vertex object • element • reference to position in vertex sequence u a • Edge object • • element origin vertex object destination vertex object reference to position in edge sequence v u c b d w z w v z • Vertex sequence • sequence of vertex objects • Edge sequence • sequence of edge objects a b c d
Adjacency List Structure a • Edge list structure • Incidence sequence for each vertex • sequence of references to edge objects of incident edges • Augmented edge objects v b u u w v w • references to associated positions in incidence sequences of end vertices a b