Algorithms CSCI 235, Spring 2019 Lecture 33 Graphs II

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

Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
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.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
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:
Tirgul 11 DFS Properties of DFS Topological sort.
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
CS 473Lecture 151 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
David Luebke 1 5/20/2015 CS 332: Algorithms Graph Algorithms.
CSE 780 Algorithms Advanced Algorithms Graph Alg. DFS Topological sort.
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/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”
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
1 Depth-First Search Idea: –Starting at a node, follow a path all the way until you cannot move any further –Then backtrack and try another branch –Do.
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.
1 Chapter 22: Elementary Graph Algorithms II. 2 About this lecture Depth First Search DFS Tree and DFS Forest Properties of DFS Parenthesis theorem (very.
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.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
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.
CS138A Elementary Graph Algorithms Peter Schröder.
64 Algorithms analysis and design BY Lecturer: Aisha Dawood.
Introduction to Algorithms
CSC 172 DATA STRUCTURES.
Breadth-First Search (BFS)
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Tracing An Algorithm for Strongly Connected Components that uses Depth First Search Graph obtained from Text, page a-al: Geetika Tewari.
Graphs-Part II Depth First Search (DFS)
Main algorithm with recursion: We’ll have a function DFS that initializes, and then calls DFS-Visit, which is a recursive function and does the depth first.
Searching Graphs ORD SFO LAX DFW Spring 2007
CSC317 Graph algorithms Why bother?
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.
CS200: Algorithm Analysis
Graphs Breadth First Search & Depth First Search
CSE 421: Introduction to 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)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Advanced Algorithms Analysis and Design
Graph Representation (23.1/22.1)
Graphs Part 2 Adjacency Matrix
Depth-First Search Graph Traversals Depth-First Search DFS.
CSE 421: Introduction to Algorithms
Subgraphs, Connected Components, Spanning Trees
Searching Graphs ORD SFO LAX DFW Spring 2007
A Introduction to Computing II Lecture 13: Trees
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Copyright © Aiman Hanna All rights reserved
Algorithms Searching in a Graph.
Algorithms CSCI 235, Spring 2019 Lecture 34 Graphs III
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application may require that vertices be visited in some.
Presentation transcript:

Algorithms CSCI 235, Spring 2019 Lecture 33 Graphs II

Depth First Search Idea: Explore the graph from a vertex by searching as far (deep) as you can along a given path. When you have moved as far as you can go, back up until you find a vertex with unexplored neighbors. Follow these paths to the end. Continue until all vertices reachable from the original have been explored. If any undiscovered vertices remain (e.g. if the graph is unconnected), one of them is selected as a new source and the search is repeated. Repeat until all vertices are discovered. Note that DFS may generate multiple trees (i.e. a forest).

Pseudocode for DFS DFS(G) for each vertex u in G.V //Initialize vertices u.color = white u.pred = NIL for each vertex u in G.V if u.color == white //If any vertices not discovered DFS-Visit(G, u) //Start a new tree with them DFS-Visit(G, u) u.color = gray for each v in G.Adj[u] if v.color == white v.pred = u DFS-Visit(G, v) u.color = black

Example r s t u v w x y We will apply DFS to this graph in class.

Analysis of DFS Initialization takes: Q(v) DFS-Visit called once on each vertex:Q (v) DFS-Visit explores each edge exactly once: Q(E) Total: Q(V+E)

Classifying Edges DFS can be used to classify the edges of G: An edge, (u, v) is a: Tree edge: if v was first discovered by exploring the edge (u, v). (u, v) is an edge in the tree formed by depth-first-search. Back edge: if it is a non-tree edge in which v is an ancestor of u. Forward edge: if it is a non-tree edge in which v is a descendent of u. Cross edge: if it is a non-tree edge that connects two vertices that do not have an ancestor/descendant relationship, or that are in two different trees.

Classifying edges during DFS We can classify the edges of a graph during the DFS algorithm the following way: If v is white when first examined from u, then (u, v) is a tree edge. If v is gray when first examined from u, then (u, v) is a back edge. If v is black when first examined from u, then (u, v) is either a forward edge or a cross edge.

Ambiguities for undirected graphs There is some ambiguity between back and forward edges for an undirected graph. The same edge can be classified as back or forward depending on which vertex is listed first (u, v) or (v, u). We classify these edges according to the first type of edge from the above list that is applicable (e.g. back edge). This is equivalent to classifying it according to whether (u, v) or (v, u) is encountered first in the DFS algorithm. It can be shown that undirected graphs never have forward or cross edges.

Example 1: previous graph t u v s w x v w x y t Graph DFS tree u Tree edges: (r, v), (r, s), (s, w), (w, x), (x, t), (t, u), (u, y) (t, w) is a back edge (y, x) is a back edge y

Example 2 z s w v We will work this out in class.