22
Elementary Graph Algorithms Chapter 22

Graphs

Embed Size (px)

DESCRIPTION

Powerpoint about Graphs

Citation preview

Page 1: Graphs

Elementary Graph Algorithms

Chapter 22

Page 2: Graphs

Graph Basics

• A graph is a set of vertices (points) and edges (connections between points)

• General notation: G = (V, E)• A graph is directed if the edges

are one-directional (contain an arrowhead)

• A graph is undirected if the edges indicate no direction (do not contain an arrowhead)

Page 3: Graphs

Representations of Graphs

• Adjacency list– Array of lists, one for each vertex

• Adjacency matrix– Matrix or two-dimensional grid, rows and columns

represent vertices

Page 4: Graphs

Adjacency List• Array of lists, one for each vertex• For each vertex u, the list for u contains all other

vertices v such that there is an edge (u, v), i.e., all vertices adjacent to u in G

Undirected graph: Directed graph:

Page 5: Graphs

Size of Adjacency Lists

• For a directed graph, number of elements among all adjacency lists is |E|

• For an undirected graph, number of elements among all adjacency lists is 2|E|

• Amount of memory required is θ(V+E)

Page 6: Graphs

Adjacency Matrix• Matrix of size |V|x|V|• Rows and columns represent vertices• For row i and column j, matrix entry (i, j) is 1 if (i, j) is

an edge, and 0 otherwise

Undirected graph: Directed graph:

Page 7: Graphs

Size of Adjacency Matrices

• Requires θ(V2) memory, independent of the number of edges

• For an undirected graph, there is symmetry along the main diagonal, thus can store only the entries on and above the diagonal

Page 8: Graphs

Adjacency List vs. Adjacency Matrix

• Adjacency list– More compact – uses less space– Good for representing sparse graphs (few edges)– Not easy to tell if an edge exists between two given

vertices– More commonly used

• Adjacency matrix– Uses more space– Good for representing dense graphs (many edges)– Can easily tell if an edge exists between two given vertices– Simpler, so good for small graphs

Page 9: Graphs

Breadth-First Search• Given a graph G and a source vertex s, find every

vertex that is reachable from s• Computes distance (smallest number of edges)

from s to each reachable vertex• Explores all vertices at distance k from s before

exploring at distance k+1 (i.e., spans the width)• Produces breadth-first tree with root s containing

all reachable vertices• Simple path from s to v in this tree corresponds

to a shortest path from s to v in G (i.e., path containing the smallest number of edges)

Page 10: Graphs

BFS AlgorithmBFS(G, s)1 for each vertex u in G.V – {s}2 u.color = WHITE // mark as unseen3 u.d = ∞ // distance from s4 u.π = NIL // predecessor5 s.color = GRAY // mark as seen6 s.d = 07 s.π = NIL8 Q = Ø // FIFO queue9 ENQUEUE(Q, s) // insert source vertex10 while Q ≠ Ø11 u = DEQUEUE(Q)12 for each v in G.Adj[u] // for each of u’s neighbors13 if v.color == WHITE // if unseen14 v.color = GRAY // mark as seen15 v.d = u.d + 116 v.π = u17 ENQUEUE(Q, v)18 u.color = BLACK // mark as done, all neighbors have been examined

Page 11: Graphs

BFS Examples

• Source vertex: s

• Source vertex: 3

Page 12: Graphs

Run time of BFS

• Each enqueuing and dequeuing: O(1) time• Total time for queue operations: O(V)• Scanning adjacency lists: O(E)• Initialization: O(V)• Total running time: O(V+E)• Linear in the size of the adjacency list

representation of G

Page 13: Graphs

Printing the Shortest Path

• Runs in time linear in the number of vertices in the path

PRINT-PATH(G, s, v)1 if v == s2 print s3 else if v.π == NIL4 print “no path from “ s “ to “ v “ exists”5 else PRINT-PATH(G, s, v.π)6 print v

Page 14: Graphs

Depth-First Search• Search “deeper” whenever possible• Explore edges out of the most recently seen

vertex v that still has unexplored edges leaving it• After exploring all of v’s edges, backtrack to

explore edges leaving the vertex from which v was first seen

• Continue until all vertices have been seen• Can search from multiple sources• Produces a depth-first forest with possibly

multiple depth-first trees

Page 15: Graphs

DFS AlgorithmDFS(G)1 for each vertex u in G.V2 u.color = WHITE3 u.π = NIL4 time = 05 for each vertex u in G.V6 if u.color == WHITE // if unseen7 DFS-VISIT(G, u) // explore paths out of u

DFS-VISIT(G, u)1 time = time + 1 // white vertex u has just been discovered2 u.d = time // discovery time of u3 u.color = GRAY // mark as seen4 for each v in G.Adj[u] // explore edge (u, v)5 if v.color == WHITE // if unseen6 v.π = u7 DFS-VISIT(G, v) // explore paths out of v (i.e., go “deeper”)8 u.color = BLACK // u is finished9 time = time + 110 u.f = time // finish time of u

Page 16: Graphs

DFS Examples

• Source vertex: 1

• Source vertex: s

Page 17: Graphs

Run time of DFS

• Loops in DFS: θ(V)• DFS-VISIT called for each vertex in V• Adjacency list scans in DFS-VISIT: θ(E)• Total run time: θ(V+E)

Page 18: Graphs

Topological Sort

• Linear ordering of all vertices such that for each edge (u, v), u appears before v in the ordering

• Ordering of vertices along a horizontal line so that all directed edges go from left to right

• Indicates precedences among events• Graph must be directed and acyclic (DAG)

Page 19: Graphs

Topological Sort AlgorithmDFS(G)1 for each vertex u in G.V2 u.color = WHITE3 u.π = NIL4 time = 05 for each vertex u in G.V6 if u.color == WHITE // if unseen7 DFS-VISIT(G, u) // explore paths out of u

DFS-VISIT(G, u)1 time = time + 1 // white vertex u has just been discovered2 u.d = time // discovery time of u3 u.color = GRAY // mark as seen4 for each v in G.Adj[u] // explore edge (u, v)5 if v.color == WHITE // if unseen6 v.π = u7 DFS-VISIT(G, v) // explore paths out of v (i.e., go “deeper”)8 u.color = BLACK // u is finished9 time = time + 110 u.f = time // finish time of u11 toposort-list.prepend(u)

Page 20: Graphs

Example

• Getting dressed in the morning• Must put on certain garments before others

(e.g., socks before shoes)• Others can be put on in any order (e.g., socks

and pants)• Directed edge (u, v) indicates garment u must

be put on before garment v• Topological sort gives order for getting

dressed

Page 21: Graphs

Example

• Topologically sorted vertices appear in reverse order of their finish times

Page 22: Graphs

Another Example

1

4

7

2

5

8

3

6

9

10