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)

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

1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
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.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
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.
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
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 & BFS.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Introduction to Graph  A graph consists of a set of vertices, and a set of edges that link together the vertices.  A graph can be: Directed: Edges are.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Lecture 15: Depth First Search Shang-Hua Teng. Graphs G= (V,E) B E C F D A B E C F D A Directed Graph (digraph) –Degree: in/out Undirected Graph –Adjacency.
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| =
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.
COSC 3101A - Design and Analysis of Algorithms 10
Spring 2015 Lecture 10: Elementary Graph Algorithms
CS 3343: Analysis of Algorithms Lecture 21: Introduction to Graphs.
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
David Luebke 1 10/16/2015 CS 332: Algorithms Go Over Midterm Intro to Graph Algorithms.
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.
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 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|
Chapter 22: Elementary Graph Algorithms
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.
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
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.
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.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
Graphs Breadth First Search & Depth First Search
CS 201: Design and Analysis of Algorithms
Elementary Graph Algorithms
Graphs Breadth First Search & Depth First Search
CS 3343: Analysis of Algorithms
CS120 Graphs.
Graph.
Elementary Graph 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)
Graph & BFS.
Finding Shortest Paths
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Basic Graph Algorithms
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

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) is same as (v,u) –Thus |E| <= |V| (|V|-1)/2 A ONM LKJ EFGH DCB I P

Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, ordered pairs of vertices from V  V –(u,v) is different from (v,u) –Thus |E| <= |V| (|V|-1) A EFGH DCB I

Basic Graph Properties An undirected graph is connected if it has a path from every vertex to every other A directed graph is strongly connected if it has a path from every vertex to every other A weighted graph associates weights with either the edges or the vertices E.g., a road map: edges might be weighted w/ distance A multigraph allows multiple edges between the same vertices

Dense and Sparse Graphs We will typically express running times in terms of |E| and |V| (often dropping the |’s) –If |E|  |V| 2 the graph is dense –If |E|  |V| the graph is sparse Different data structures may be used to express sparse and dense graph

Graph in Applications Internet: web graphs –Each page is a vertex –Each edge represent a hyperlink –Directed GPS: highway maps –Each city is a vertex –Each freeway segment is an undirected edge –Undirected Graphs are ubiquitous in computer science

Representing Graphs Assume V = {1, 2, …, n} An adjacency matrix represents the graph as a n x n matrix A: –A[i, j] = 1 if edge (i, j)  E (or weight of edge) = 0 if edge (i, j)  E

Adjacency Matrix Example: a d bc A

Adjacency Matrix Representation Undirected graph  matrix A is symmetric Storage requirements: O(V 2 ) –A dense representation The adjacency matrix is a dense representation –Usually too much storage for large graphs –But can be very efficient for small graphs Most large interesting graphs are sparse –For example, trees and planar graphs (such as highway map) have |E| = O(|V|) –For this reason, we often needs more sparse representation

Adjacency List Representation Adjacency list: for each vertex v  V, store a list of vertices adjacent to v Example: –Adj[1] = {2}{3} –Adj[2] = {3} –Adj[3] = {} –Adj[4] = {3} Variation: can also keep a list of edges coming into vertex

Adjacency List Representation The degree of a vertex v in an undirected graph is equal to the number of incident edges For directed graphs, we can define in-degree, out-degree for each vertex For directed graphs, the total size of the adjacency lists is  out-degree(v) = |E| takes  (V + E) storage For undirected graphs, the total size of the adjacent lists is  degree(v) = 2 |E| also  (V + E) storage So: Adjacency lists take O(V+E) storage

Google Problem:Graph Searching How to search and explore in the Web Space? How to find all web-pages and all the hyperlinks? –Start from one vertex “ –Systematically follow hyperlinks from the discovered vertex/web page –Build a search tree along the exploration

General Graph Searching Input: a graph G = (V, E), directed or undirected Objective: methodically explore every vertex and every edge 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 Objective: Traverse all vertices of a graph G that can be reached from a starting vertex, called root Method: –Search for all vertices that are directly reachable from the root (called level 1 vertices) –After mark all these vertices, visit all vertices that are directly reachable from any level 1 vertices (called level 2 vertices), and so on. –In general level k vertices are directly reachable from a level k – 1 vertices

An Example

A ONM LKJ EFGH DCB I P 0

A ONM LKJ EFGH DCB I P

A ONM LKJ EFGH DCB I P

A ONM LKJ EFGH DCB I P

A ONM LKJ EFGH DCB I P

A ONM LKJ EFGH DCB I P

A ONM LKJ EFGH DCB I P

A ONM LKJ EFGH DCB I P

Breadth-First Search BFS(G, s) 1.For each vertex u in V – {s}, 2. color[u] = white; d[u] = infty;  [u] = NIL 3.color[s] = GRAY; d[s] = 0;  [s] = NIL; Q = {} 4.ENQUEUE(Q,s) // Q is a FIFO queue 5.while (Q not empty) 6. u = DEQUEUE(Q) 7. for each v  Adj[u] 8. if color[v] = WHITE 9. then color[v] = GREY 10. d[v] = d[u] + 1;  [v] = u 11. ENQUEUE(Q, v); 12. color[u] = BLACK;

Breadth-first Search Algorithm 1.Algorithm BFS(s): 1.initialize container L to contain start vertex s. 2.i  0 3.while L i is not empty do 1.create container L i+1 to initially be empty 2.for each vertex v in L i do 1.for each edge e incident on v do 1.if edge e is unexplored then 2. let w be the other endpoint of e 3. if vertex w is unexplored then 4. label e as a discovery edge 5.insert w into L i+1 6. else 7.label e as a cross edge 4.i  i + 1

Breadth-first Search Visited all vertices reachable from the root A spanning tree For any vertex at level i, the spanning tree path from s to i has i edges, and any other path from s to i has at least i edges (shortest path property) Edges not in the spanning tree are at most, 1 level apart (level by level property)

Breadth-First Search: the Color Scheme 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

Example         rstu vwxy

  0      rstu vwxy s Q:

Example 1  0 1     rstu vwxy w Q: r

Example 1    rstu vwxy r Q: tx

Example   rstu vwxy Q: txv

Example  rstu vwxy Q: xvu

Example rstu vwxy Q: vuy

Example rstu vwxy Q: uy

Example rstu vwxy Q: y

Example rstu vwxy Q: Ø

Properties of BFS 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 –Prove using blackboard 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

39 Properties of BFS Proposition: Let G be an undirected graph on which a BFS traversal starting at vertex s has been performed. Then –The traversal visits all vertices in the connected component of s. –The discovery-edges form a spanning tree T, which we call the BFS tree, of the connected component of s –For each vertex v at level i, the path of the BFS tree T between s and v has i edges, and any other path of G between s and v has at least i edges. –If (u, v) is an edge that is not in the BFS tree, then the level numbers of u and v differ by at most one.

40 Properties of BFS Proposition: Let G be a graph with n vertices and m edges. A BFS traversal of G takes time O(n + m). Also, there exist O(n + m) time algorithms based on BFS for the following problems: –Testing whether G is connected. –Computing a spanning tree of G –Computing the connected components of G –Computing, for every vertex v of G, the minimum number of edges of any path between s and v.