CSC 172 DATA STRUCTURES.

Slides:



Advertisements
Similar presentations
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
Advertisements

Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Depth-First Search1 DB A C E. 2 Outline and Reading Definitions (§6.1) Subgraph Connectivity Spanning trees and forests Depth-first search (§6.3.1) Algorithm.
Breadth-First Search1 Part-H3 Breadth-First Search CB A E D L0L0 L1L1 F L2L2.
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 Foundations of Software Design Fall 2002 Marti Hearst Lecture 25: Graph Traversal, DAGs, and Weighted Graphs.
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
Graph Traversals CSC 172 SPRING 2002 LECTURE 26. Traversing graphs Depth-First Search like a post-order traversal of a tree Breath-First Search Less like.
Graph Traversals CSC 172 SPRING 2004 LECTURE 21. Announcements  Project 3 is graded  handed back Tuesday  Grad spam, tonight – if you are really anxious.
Depth-First Search1 DB A C E. 2 Depth-first search (DFS) is a general technique for traversing a graph A DFS traversal of a graph G – Visits all the vertices.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Depth-First Search Lecture 21: Graph Traversals
Graphs and Paths : Chapter 15 Saurav Karmakar
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
© 2010 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search L0 L1 L2
Graphs – Breadth First Search
A vertex u is reachable from vertex v iff there is a path from v to u.
Generic DFS and BFS Template Method Pattern Generic DFS
Searching Graphs ORD SFO LAX DFW Spring 2007
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Csc 2720 Instructor: Zhuojun Duan
Breadth-First Search L0 L1 L2
Lecture 12 Graph Algorithms
Chapter 14 Graph Algorithms
CC 215 Data Structures Graph Searching
CS120 Graphs.
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Depth-First Search D B A C E Depth-First Search Depth-First Search
CSC 172 DATA STRUCTURES.
CSE 421: Introduction to Algorithms
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search (BFS)
Depth-First Search D B A C E Depth-First Search Depth-First Search
Graphs Representation, BFS, DFS
Elementary Graph Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Depth-First Search D B A C E Depth-First Search Depth-First Search
Depth-First Search Graph Traversals Depth-First Search DFS.
Graphs ORD SFO LAX DFW Graphs Graphs
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Subgraphs, Connected Components, Spanning Trees
Depth-First Search D B A C E Depth-First Search Depth-First Search
Searching Graphs ORD SFO LAX DFW Spring 2007
COMP171 Depth-First Search.
Copyright © Aiman Hanna All rights reserved
Depth-First Search CSE 2011 Winter April 2019.
Depth-First Search D B A C E 4/13/2019 5:23 AM Depth-First Search
Algorithms: Design and Analysis
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search L0 L1 L2 C B A E D F 4/25/2019 3:12 AM
Depth-First Search CSE 2011 Winter April 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
3.2 Graph Traversal.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Elementary Graph Algorithms
Breadth-First Search L0 L1 L2 C B A E D F 5/14/ :22 AM
Lecture 10 Graph Algorithms
Lecture 11 Graph Algorithms
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
Presentation transcript:

CSC 172 DATA STRUCTURES

Traversing graphs Depth-First Search Breath-First Search like a pre- or post-order traversal of a tree (use stack) Breath-First Search like a level-order traversal (use queue)

Exploring a Maze A depth-first search (DFS) in an undirected graph G is like wandering in a maze with a string and a can of paint – you can prevent yourself from getting lost.

Example A B C D E F G H I J K L M N O P

DFS Start at vertex s Travel along an arbitrary edge (u,v) Tie the end of the string to s and mark “visited” on s Make s the current vertex u Travel along an arbitrary edge (u,v) unrolling string If edge(u,v) leads to an already visited vertex v then return to u else mark v as “visited”, set v as current u, repeat @ step 2 When all edges lead to visited vertices, backtrack to previous vertex (roll up string) and repeat @ step 2 When we backtrack to s and explore all its edges we are done

DFS Pseudocode (labels edges) DFS( Vertex v) for each edge incident on v do: if edge e is unexplored then let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge recursively call DFS(w) else label e as a backedge

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

DFS Tree A B C D E F G H I J K L M N O P

DFS Properties Starting at s The traversal visits all the vertices in the connected component of s The discovery edges form a spanning tree of the connected component of s

DFS Runtime DFS is called on each vertex exactly once Every edge is examined exactly twice (once from each of its vertices) So, for ns vertices and ms edges in the connected component of the vertex s, the DFS runs in O(ns+ms) if: - The graph data structure methods take constant time - Marking takes constant time - There is a systematic way to examine edges (avoiding redundancy)

Marking Vertices Extend vertex structure to support variable for marking Use a hash table mechanism to log marked vertices

Breadth-First Search Starting vertex has level 0 (anchor vertex) Visit (mark) all vertices that are only one edge away mark each vertex with its “level” One edge away from level 0 is level 1 One edge away from level 1 is level 2 Etc. . . .

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example A B C D E F G H I J K L M N O P

Example 1 A B C D E F G H I J K L M N O P

Example 1 A B C D E F G H I J K L M N O P

Example 2 1 A B C D E F G H I J K L M N O P

Example 2 1 A B C D E F G H I J K L M N O P

Example 2 1 3 A B C D E F G H I J K L M N O P

Example 2 1 3 A B C D E F G H I J K L M N O P

Example 2 1 3 A B C D E F G H I J K 4 L M N O P

Example 2 1 3 A B C D E F G H I J K 4 L M N O P

Example 2 1 3 A B C D E F G H I J K 4 L M N O P

Example 2 1 3 A B C D E F G H I J K 4 L M N O P

Example 2 1 3 A B C D E F G H I J K 4 L 5 M N O P

Example 2 1 3 A B C D E F G H I J K 4 L 5 M N O P

BFS Tree 2 1 3 A B C D E F G H I J K 4 L 5 M N O P

BFS Pseudocode (1 of 2) BFS(Vertex s) initialize container L0 to contain vertex s i  0 while Li is not empty do create container Li+1 initially empty for each vertex v in Li do // next slide i  i+1

BFS Pseudocode (2 of 2) // for each vertex v in Li do if edge e incident on v do let w be the other endpoint of e if w is unexplored then label e as a discovery edge insert w into Li+1 else label e as a cross edge //i  i+1

BFS Properties The traversal visits all vertices in the connected component of s The discover edges form a spanning tree of the cc 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 number of u and v differ by at most one

Run Time A BFS traversal takes O(n+m) time Also, there exist O(n+m) time algorithms based on BFS which test for Connectivity of graph Spanning tree of G Connected component Shortest path between s and v