Elementary Graph Algorithms

Slides:



Advertisements
Similar presentations
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Advertisements

Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Graphs Searching. Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately:
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Shortest Path Problems
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
1 7/2/2015 ITCS 6114 Graph Algorithms. 2 7/2/2015 Graphs ● A graph G = (V, E) ■ V = set of vertices ■ E = set of edges = subset of V  V ■ Thus |E| =
1 7/3/2015 ITCS 6114 Graph Algorithms. 2 7/3/2015 Depth-First Search ● Depth-first search is another strategy for exploring a graph ■ Explore “deeper”
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
David Luebke 1 10/1/2015 CS 332: Algorithms Topological Sort Minimum Spanning Tree.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
CSC 413/513: Intro to Algorithms Graph Algorithms DFS.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Elementary Graph Algorithms Comp 122, Fall 2004.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CSC 413/513: Intro to Algorithms Graph Algorithms.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
Mudasser Naseer 1 1/9/2016 CS 201: Design and Analysis of Algorithms Lecture # 17 Elementary Graph Algorithms (CH # 22)
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Liaquat Majeed Sheikh 1 1/25/2016 Graph Algorithms.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Chapter 05 Introduction to Graph And Search Algorithms.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
CS138A Elementary Graph Algorithms Peter Schröder.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
CS 201: Design and Analysis of Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Data Structures, Algorithms & Complexity
CSC317 Graph algorithms Why bother?
Topological Sort Minimum Spanning Tree
Depth-First Search Depth-first search is a strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the.
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Many slides here are based on E. Demaine , D. Luebke slides
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph Representation Adjacency list representation of G = (V, E)
Finding Shortest Paths
BFS,DFS Topological Sort
Graphs Chapter 15 explain graph-based algorithms Graph definitions
CS6045: Advanced Algorithms
Graph Representation (23.1/22.1)
Basic Graph Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Elementary Graph Algorithms Graphs Elementary Graph Algorithms

Graphs A graph G = (V, E) V = set of vertices E = set of edges = subset of V  V Thus |E| = O(|V|2)

Graph Variations Variations: A connected graph has a path from every vertex to every other In an undirected graph: Edge (u,v) = edge (v,u) No self-loops In a directed graph: Edge (u,v) goes from vertex u to vertex v, notated uv

Graph Variations More variations: A weighted graph associates weights with either the edges or the vertices E.g., a road map: edges might be weighted with distance A multigraph allows multiple edges between the same vertices E.g., the call graph in a program (a function can get called from multiple points in another function)

Representation of Graphs Two standard ways. Adjacency Lists. Adjacency Matrix. a d c b a d c b 1 2 3 4 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0 Comp 122, Fall 2004

Adjacency Lists Consists of an array Adj of |V| lists. One list per vertex. For u  V, Adj[u] consists of all vertices adjacent to u. a b a b d c b c If weighted, store weights also in adjacency lists. c d c d d a d c b Comp 122, Fall 2004

Storage Requirement For directed graphs: out-degree(v) = |E| Sum of lengths of all adj. lists is out-degree(v) = |E| vV Total storage: (V+E) For undirected graphs: degree(v) = 2|E| No. of edges leaving v No. of edges incident on v. Edge (u,v) is incident on vertices u and v. Comp 122, Fall 2004

Pros and Cons: adj list Pros Cons Space-efficient, when a graph is sparse. Can be modified to support many graph variants. Cons Determining if an edge (u,v) G is not efficient. Have to search in u’s adjacency list. (degree(u)) time. (V) in the worst case. Comp 122, Fall 2004

Adjacency Matrix |V|  |V| matrix A. Number vertices from 1 to |V| in some arbitrary manner. A is then given by: 1 2 1 2 3 4 1 0 1 1 1 2 0 0 1 0 3 0 0 0 1 4 0 0 0 0 a b c d 4 3 a d c b 1 2 3 4 1 2 3 4 1 0 1 1 1 2 1 0 1 0 3 1 1 0 1 4 1 0 1 0 A = AT for undirected graphs. Comp 122, Fall 2004

Space and Time Space: (V2). Not memory efficient for large graphs. Time: to list all vertices adjacent to u: (V). Time: to determine if (u, v)  E: (1). Can store weights instead of bits for weighted graph. Comp 122, Fall 2004

Graph Searching Given: a graph G = (V, E), directed or undirected Goal: methodically explore every vertex and every edge Ultimately: build a tree on the graph Pick a vertex as the root Choose certain edges to produce a tree Note: might also build a forest if graph is not connected

Breadth-First Search “Explore” a graph, turning it into a tree One vertex at a time Expand frontier of explored vertices across the breadth of the frontier Builds a tree over the graph Pick a source vertex to be the root Find (“discover”) its children, then their children, etc.

Breadth-First Search Will associate vertex “colors” to guide the algorithm White vertices have not been discovered All vertices start out white Grey vertices are discovered but not fully explored They may be adjacent to white vertices Black vertices are discovered and fully explored They are adjacent only to black and gray vertices Explore vertices by scanning adjacency list of grey vertices

Breadth-First Search What does d[v] represent? BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue ; initialize to s while (Q not empty) { u = Remove(Q); for each v  adj[u] { if (color[v] == WHITE) color[v] = GREY; d[v] = d[u] + 1; p[v] = u; Enqueue(Q, v); } color[u] = BLACK; What does d[v] represent? What does p[v] represent?

Breadth-First Search: Example u         v w x y

Breadth-First Search: Example u        v w x y Q: s

Breadth-First Search: Example u 1    1   v w x y Q: w r

Breadth-First Search: Example u 1 2   1 2  v w x y Q: r t x

Breadth-First Search: Example u 1 2  2 1 2  v w x y Q: t x v

Breadth-First Search: Example u 1 2 3 2 1 2  v w x y Q: x v u

Breadth-First Search: Example u 1 2 3 2 1 2 3 v w x y Q: v u y

Breadth-First Search: Example u 1 2 3 2 1 2 3 v w x y Q: u y

Breadth-First Search: Example u 1 2 3 2 1 2 3 v w x y Q: y

Breadth-First Search: Example u 1 2 3 2 1 2 3 v w x y Q: Ø

BFS: The Code Again Touch every vertex: O(V) BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue ; initialize to s while (Q not empty) { u = Remove(Q); for each v  adj[u] { if (color[v] == WHITE) color[v] = GREY; d[v] = d[u] + 1; p[v] = u; Enqueue(Q, v); } color[u] = BLACK; Touch every vertex: O(V) u = every vertex, but only once (Why?) So v = every vertex that appears in some other vert’s adjacency list What will be the running time? Total running time: O(V+E)

BFS: The Code Again What will be the storage cost BFS(G, s) { initialize vertices; Q = {s}; // Q is a queue ; initialize to s while (Q not empty) { u = Remove(Q); for each v  adj[u] { if (color[v] == WHITE) color[v] = GREY; d[v] = d[u] + 1; p[v] = u; Enqueue(Q, v); } color[u] = BLACK; What will be the storage cost in addition to storing the tree? Total space used: O(max(degree(v))) = O(E)

Breadth-First Search: Properties BFS calculates the shortest-path distance to the source node Shortest-path distance (s,v) = minimum number of edges from s to v, or  if v not reachable from s BFS builds breadth-first tree, in which paths to root represent shortest paths in G Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time

Depth-First Search Depth-first search is another strategy for exploring a graph Explore “deeper” in the graph whenever possible Edges are explored out of the most recently discovered vertex v that still has unexplored edges When all of v’s edges have been explored, backtrack to the vertex from which v was discovered

Depth-First Search Vertices initially colored white Then colored gray when discovered Then black when finished

Depth-First Search: The Code DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time;

Depth-First Search: The Code DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; What does d[u] represent?

Depth-First Search: The Code DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; What does f[u] represent?

Depth-First Search: The Code DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; Will all vertices eventually be colored black?

Depth-First Search: The Code DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; What will be the running time?

Depth-First Search: The Code DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; Running time: O(n2) because call DFS_Visit on each vertex, and the loop over Adj[] can run as many as |V| times

Depth-First Search: The Code DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; BUT, there is actually a tighter bound. How many times will DFS_Visit() actually be called?

Depth-First Search: The Code DFS(G) { for each vertex u  V[G] color[u] = WHITE; } time = 0; if (color[u] == WHITE) DFS_Visit(u); DFS_Visit(u) { color[u] = GREY; time = time+1; d[u] = time; for each v  Adj[u] if (color[v] == WHITE) DFS_Visit(v); } color[u] = BLACK; f[u] = time; So, running time of DFS = O(V+E)

Depth-First Sort Analysis This running time argument is an informal example of amortized analysis “Charge” the exploration of edge to the edge: Each loop in DFS_Visit can be attributed to an edge in the graph Runs once/edge if directed graph, twice if undirected Thus loop will run in O(E) time, algorithm O(V+E) Considered linear for graph, b/c adj list requires O(V+E) storage Important to be comfortable with this kind of reasoning and analysis

DFS Example source vertex

DFS Example source vertex d f 1 | | | | | | | |

DFS Example source vertex d f 1 | | | 2 | | | | |

DFS Example source vertex d f 1 | | | 2 | | 3 | | |

DFS Example source vertex d f 1 | | | 2 | | 3 | 4 | |

DFS Example source vertex d f 1 | | | 2 | | 3 | 4 5 | |

DFS Example source vertex d f 1 | | | 2 | | 3 | 4 5 | 6 |

DFS Example source vertex d f 1 | 8 | | 2 | 7 | 3 | 4 5 | 6 |

DFS Example source vertex d f 1 | 8 | | 2 | 7 | 3 | 4 5 | 6 |

What is the structure of the grey vertices? What do they represent? DFS Example source vertex d f 1 | 8 | | 2 | 7 9 | 3 | 4 5 | 6 | What is the structure of the grey vertices? What do they represent?

DFS Example source vertex d f 1 | 8 | | 2 | 7 9 |10 3 | 4 5 | 6 |

DFS Example source vertex d f 1 | 8 |11 | 2 | 7 9 |10 3 | 4 5 | 6 |

DFS Example source vertex d f 1 |12 8 |11 | 2 | 7 9 |10 3 | 4 5 | 6 |

DFS Example d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 | source vertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 |

DFS Example d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14| source vertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|

DFS Example d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|15 source vertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|15

DFS Example d f 1 |12 8 |11 13|16 2 | 7 9 |10 3 | 4 5 | 6 14|15 source vertex d f 1 |12 8 |11 13|16 2 | 7 9 |10 3 | 4 5 | 6 14|15