Subgraphs, Connected Components, Spanning Trees

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.
© 2004 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
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.
© 2004 Goodrich, Tamassia Depth-First Search1 DB A C E.
1 Graphs: Concepts, Representation, and Traversal CSC401 – Analysis of Algorithms Lecture Notes 13 Graphs: Concepts, Representation, and Traversal Objectives:
CSC311: Data Structures 1 Chapter 13: Graphs I Objectives: Graph ADT: Operations Graph Implementation: Data structures Graph Traversals: DFS and BFS Directed.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
1 Graph Algorithms Lecture 09 Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information and Computer Technology.
CSC 213 – Large Scale Programming. Today’s Goals  Make Britney sad through my color choices  Revisit issue of graph terminology and usage  Subgraphs,
Graphs Part 1. Outline and Reading Graphs (§13.1) – Definition – Applications – Terminology – Properties – ADT Data structures for graphs (§13.2) – Edge.
October 22, DFS, BFS, Biconnectivity, Digraphs 1 CS 221 West Virginia University.
Graphs. Data Structure for Graphs. Graph Traversals. Directed Graphs. Shortest Paths. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013.
GRAPHS 1. Outline 2  Undirected Graphs and Directed Graphs  Depth-First Search  Breadth-First Search.
1 prepared from lecture material © 2004 Goodrich & Tamassia COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969 WARNING This material.
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
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
© 2010 Goodrich, Tamassia Breadth-First Search1 CB A E D L0L0 L1L1 F L2L2.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
CSC 172 DATA STRUCTURES.
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
Breadth-First Search (BFS)
Graphs 10/24/2017 6:47 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Directed Graphs Directed Graphs Shortest Path 12/7/2017 7:10 AM BOS
Discrete Mathematicsq
Searching Graphs ORD SFO LAX DFW Spring 2007
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Breadth-First Search L0 L1 L2
COMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms
Lecture 12 Graph Algorithms
Chapter 14 Graph Algorithms
Directed Graphs 5/1/15 12:25:22 PM
CS120 Graphs.
Graph Algorithms Using Depth First Search
Graphs Part 1.
Biconnectivity SEA PVD ORD FCO SNA MIA 11/16/2018 2:31 AM
Graphs.
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
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
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
Graphs ORD SFO LAX DFW Graphs Graphs
Spanning Trees Longin Jan Latecki Temple University based on slides by
Depth-First Search D B A C E Depth-First Search Depth-First Search
Searching Graphs ORD SFO LAX DFW Spring 2007
Graphs Part 1 ORD SFO LAX DFW
Copyright © Aiman Hanna All rights reserved
Depth-First Search D B A C E 4/13/2019 5:23 AM Depth-First Search
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
Biconnectivity SEA PVD ORD FCO SNA MIA 5/6/2019 5:08 PM Biconnectivity
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Elementary Graph Algorithms
Biconnectivity SEA PVD ORD FCO SNA MIA 5/23/ :21 PM
Breadth-First Search L0 L1 L2 C B A E D F 5/14/ :22 AM
Lecture 11 Graph Algorithms
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
Presentation transcript:

Subgraphs, Connected Components, Spanning Trees Depth-First Search 2/4/2019 8:01 PM Subgraphs, Connected Components, Spanning Trees Depth-First Search

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 of G A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph Depth-First Search

Non connected graph with two connected components Connectivity A graph is connected if there is a path between every pair of vertices A connected component of a graph G is a maximal connected subgraph of G Connected graph Non connected graph with two connected components Depth-First Search

Trees and Forests A (free) tree is an undirected graph T such that T is connected T has no cycles This definition of tree is different from the one of a rooted tree A forest is an undirected graph without cycles The connected components of a forest are trees Tree Forest Depth-First Search

Spanning Trees and Forests A spanning tree of a connected graph is a spanning subgraph that is a tree A spanning tree is not unique unless the graph is a tree Spanning trees have applications to the design of communication networks A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree Depth-First Search

Cycles How to detect that a graph has a cycle? Hint: BFS (or DFS) Depth-First Search

Connectivity Determines whether G is connected How do we know G is not connected? Hint: BFS or DFS Depth-First Search

Connected Components Computes the connected components of G How do we find all the connected components? Depth-First Search

BFS for an entire graph Algorithm BFS(G, s) Queue  new empty queue enqueue(Queue, s) while Queue.isEmpty() v  dequeue(Queue) if getLabel(v) != VISITED visit v setLabel(v, VISITED) neighbors  getAdjacentVertices(G, v) for each w  neighbors if getLabel(w) != VISITED // “children” enqueue(Queue, w) The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm BFS(G) Input graph G Output labeling of the edges and partition of the vertices of G for each u  G.vertices() setLabel(u, UNEXPLORED) for each v  G.vertices() if getLabel(v) = UNEXPLORED BFS(G, v) Breadth-First Search

DFS for an Entire Graph The algorithm uses a mechanism for setting and getting “labels” of vertices and edges Algorithm DFS(G, v) visit v setLabel(v, VISITED) neighbors  getAdjacentVertices(G, v) for each w  neighbors if getLabel(w) != VISITED // “children” DFS(G, w) Algorithm DFS(G) Input graph G Output labeling of the edges of G as discovery edges and back edges for each u  G.vertices() setLabel(u, UNEXPLORED) for each v  G.vertices() if getLabel(v) = UNEXPLORED DFS(G, v) Depth-First Search

Spanning Tree Computes a spanning tree of G How do we find a spanning tree? Depth-First Search

BFS Example unexplored vertex visited vertex unexplored edge C B A E D L0 L1 F A unexplored vertex A visited vertex unexplored edge discovery edge cross edge L0 L0 A A L1 L1 B C D B C D E F E F Breadth-First Search

BFS Example (cont.) L0 L1 L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F C B A Breadth-First Search

BFS Example (cont.) L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F A B C D E F Breadth-First Search

DFS Example unexplored vertex visited vertex unexplored edge B A C E A A visited vertex unexplored edge discovery edge back edge D B A C E D B A C E Depth-First Search

DFS Example (cont.) D B A C E D B A C E D B A C E D B A C E Depth-First Search

Spanning Forest Computes a spanning forest of G How do we find a spanning forest? Depth-First Search

Path finding Given a start vertex s and a destination vertex z Find a path from s to z Depth-First Search

Path finding in a maze Vertex intersection, corner and dead end Edge Corridor Depth-First Search

BFS and path finding Hint: use the spanning tree Depth-First Search

BFS and path finding Use the spanning tree Start from destination Go to its parent Until source is reached Depth-First Search

BFS and path finding Use the spanning tree Start from destination Go to its parent Until source is reached The found path is the shortest in terms of number of edges --- Why? Depth-First Search

DFS and path finding Use the spanning tree Start from destination Go to its parent Until source is reached The spanning tree could be large Use DFS but faster, ideas? Depth-First Search

Path Finding find a path between two given vertices v and z Algorithm pathDFS(G, v, z, S) setLabel(v, VISITED) push(S, v) if v = z return S.elements() path = null neighbors  getAdjacentVertices(G, v) for each w  neighbors if getLabel(w) != VISITED // “child” path = pathDFS(G, w, z, S) if path != null // has path via a child return path pop(S, v) //no path via a child or no children return null find a path between two given vertices v and z call DFS(G, v) with v as the start vertex use a stack S to keep track of the path When destination vertex z is encountered, return the path as the contents of the stack Depth-First Search

DFS vs. BFS Applications DFS BFS DFS BFS Spanning forest, connected components, paths, cycles  Shortest paths Biconnected components (still connected after removing one vertex, algorithm not discussed) C B A E D L0 L1 F L2 A B C D E F DFS BFS Breadth-First Search

DFS vs. BFS (cont.) Back edge (v,w) Cross edge (v,w) DFS BFS w is an ancestor of v in the tree of discovery edges Cross edge (v,w) w is in the same level as v or in the next level C B A E D L0 L1 F L2 A B C D E F DFS BFS Breadth-First Search