Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.

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.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
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.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs – Depth First Search ORD DFW SFO LAX
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.
Graph Searching CSE 373 Data Structures Lecture 20.
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.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
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 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?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
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)
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
COSC 3101A - Design and Analysis of Algorithms 10
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.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
CSC 201: Design and Analysis of Algorithms Lecture # 18 Graph Algorithms Mudasser Naseer 1 12/16/2015.
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.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms 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.
CSE Graph Search Algorithms. CSE Graph a c b Node ~ city or computer Edge ~ road or data cable Undirected or Directed A surprisingly large.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 05 Introduction to Graph And Search Algorithms.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CS 3343: Analysis of Algorithms Lecture 24: Graph searching, Topological sort.
Introduction to Algorithms
Graphs – Breadth First Search
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CSC317 Graph algorithms Why bother?
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Graph Representation (23.1/22.1)
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.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Graphs & Paths Presentation : Part II

Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent for algorithms: – Adjacency lists. –Adjacency matrix. When expressing the running time of an algorithm, it’s often in terms of both |V| and |E|.

Adjacency lists Array Adj of |V| lists, one per vertex. Vertex u’s list has all vertices v such that (u, v) ∈ E. (Works for both directed and undirected graphs.)

Example: For an undirected graph:

Example For a directed graph: Same asymptotic space and time.

Adjacency matrix

Breadth-first search Input: Graph G = (V, E), either directed or undirected, and source vertex s ∈ V. Output: d[v] = distance (smallest # of edges) from s to v, for all v ∈ V.

Idea Idea: Send a wave out from s. –First hits all vertices 1 edge from s. –From there, hits all vertices 2 edges from s. –Etc. Use FIFO queue Q to maintain wavefront. v ∈ Q if and only if wave has hit v but has not come out of v yet.

Breadth-First Search

 rstu xvwy 

 0  rstu xvwy  s Q 0

1  01  rstu xvwy  r Q 1 w 1

1  201  rstu xvwy  w Q 1 v 2

1  2012 rstu xvwy 2  v Q 2 t 2 x 2

1  2012 rstu xvwy 2  t Q 2 x 2

rstu xvwy 2  x Q 2 u 3

rstu xvwy 23 u Q 3 y 3

rstu xvwy 23 y Q 3

rstu xvwy 23  Q

Run-time = O(V+E) Shortest-path tree –the final weight is the minimum distance –keep predecessors and get the shortest path –all BFS shortest paths make a tree

Breadth-First Search Example

Breadth-First Search Since each vertex gets a finite d value at most once, values assigned to vertices are monotonically increasing over time. Time = O(V + E). –O(V) because every vertex enqueued at most once. –O(E) because every vertex dequeued at most once and we examine (u, v) is in E only when u is dequeued. Therefore, every edge examined at most once if directed, at most twice if undirected.

Depth-first search Input: G = (V, E), directed or undirected. No source vertex given! Output: 2 timestamps on each vertex: d[v] = discovery time f [v] = finishing time

Depth-first search Will methodically explore every edge. –Start over from different vertices as necessary. As soon as we discover a vertex, explore from it. –Unlike BFS, which puts a vertex on a queue so that we explore from it later. As DFS progresses, every vertex has a color: –WHITE = undiscovered –GRAY = discovered, but not finished (not done exploring from it) –BLACK = finished (have found everything reachable from it) Discovery and finish times: Unique integers from 1 to 2|V|. For all v, d[v] < f [v]. In other words, 1 ≤ d[v] < f [v] ≤ 2|V|.

Depth-First Search Methodically explore all vertices and edges All vertices are White, t = 0 For each vertex u do if u is White then Visit(u) Procedure Visit(u) color u Gray; d[u]  t  t +1 for each v adjacent to u do if v is White then Visit(v) color u Black f [u]  t  t +1 Gray vertices = stack of recursive calls

Depth-First Search

Review: 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

DFS Applications. Testing whether graph is connected. Computing a spanning forest of graph. Computing a path between two vertices of graph or equivalently reporting that no such path exists. Computing a cycle in graph or equivalently reporting that no such cycle exists.

DFS vs. BFS DFS seems cleaner than BFS: recursive, it doesn't make use of an external data structure (a queue), and is shorter too. But it is important to realize these two algorithms address very different needs. An example of BFS usage is calculating shortest paths. A Depth First Search performed on the World Wide Web would quickly overload any given web server with an ever growing number of requests. Therefore a Breadth First Search is preferred, since it dispatches requests to many servers at a time.

DFS vs. BFS ApplicationsDFSBFS Spanning forest, connected components, paths, cycles  Shortest paths  Biconnected components 

Example 2

Example 2 animated source vertex

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

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

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

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

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

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

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

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

DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 | source vertex d f What is the structure of the yellow vertices? What do they represent?

DFS Example 1 |8 | | |5 | 63 | 4 2 | 79 |10 source vertex d f

DFS Example 1 |8 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

DFS Example 1 |128 |11 | |5 | 63 | 4 2 | 79 |10 source vertex d f

DFS Example 1 |128 |1113| |5 | 63 | 4 2 | 79 |10 source vertex d f

DFS Example 1 |128 |1113| 14|5 | 63 | 4 2 | 79 |10 source vertex d f

DFS Example 1 |128 |1113| 14|155 | 63 | 4 2 | 79 |10 source vertex d f

DFS Example 1 |128 |1113|16 14|155 | 63 | 4 2 | 79 |10 source vertex d f

Snapshot