Elementary Graph Algorithms

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.
Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan Mohammad Reza Akhavan.
Data Structures Using C++
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
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.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
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.
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.
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
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
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.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
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
Graphs A New Data Structure
Graphs – Breadth First Search
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.
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
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
Depth-First Search.
CS120 Graphs.
Graph Algorithms Using Depth First Search
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
Depth-First Search D B A C E Depth-First Search Depth-First Search
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Graph Representation (23.1/22.1)
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
Connected Components, Directed Graphs, Topological Sort
Graphs ORD SFO LAX DFW Graphs Graphs
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Richard Anderson Autumn 2016 Lecture 5
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
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
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
A vertex u is reachable from vertex v iff there is a path from v to u.
Elementary Graph Algorithms
Richard Anderson Winter 2019 Lecture 6
Breadth-First Search L0 L1 L2 C B A E D F 5/14/ :22 AM
Richard Anderson Winter 2019 Lecture 5
Breadth-First Search L0 L1 L2 C B A E D F 7/28/2019 1:03 PM
Richard Anderson Autumn 2015 Lecture 6
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:

Elementary Graph Algorithms

Paths and Cycles A path is a sequence of vertices P = (v0, v1, …, vk) such that, for 1 ≤ i ≤ k, edge (vi – 1, vi) ∈ E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v0, v1, …, vk – 1) such that, for 0 ≤ i < k, edge (vi, v(i + 1) mod k) ∈ E. Cycle C is simple if the path (v0, v1, vk – 1) is simple. a d h b g j e f c i

Paths and Cycles A path is a sequence of vertices P = (v0, v1, …, vk) such that, for 1 ≤ i ≤ k, edge (vi – 1, vi) ∈ E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v0, v1, …, vk – 1) such that, for 0 ≤ i < k, edge (vi, v(i + 1) mod k) ∈ E. Cycle C is simple if the path (v0, v1, vk – 1) is simple. P1 = (g, d, e, b, d, a, h) is not simple. a d h b g j e f c i P2 = (f, i, c, j) is simple.

Paths and Cycles A path is a sequence of vertices P = (v0, v1, …, vk) such that, for 1 ≤ i ≤ k, edge (vi – 1, vi) ∈ E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v0, v1, …, vk – 1) such that, for 0 ≤ i < k, edge (vi, v(i + 1) mod k) ∈ E. Cycle C is simple if the path (v0, v1, vk – 1) is simple. C1 = (a, h, j, c, i, f, g, d) is simple. a d h b g j e f c i

Paths and Cycles A path is a sequence of vertices P = (v0, v1, …, vk) such that, for 1 ≤ i ≤ k, edge (vi – 1, vi) ∈ E. Path P is simple if no vertex appears more than once in P. A cycle is a sequence of vertices C = (v0, v1, …, vk – 1) such that, for 0 ≤ i < k, edge (vi, v(i + 1) mod k) ∈ E. Cycle C is simple if the path (v0, v1, vk – 1) is simple. C1 = (a, h, j, c, i, f, g, d) is simple. a d h b g j e f c i C2 = (g, d, b, h, j, c, i, e, b) is not simple.

Subgraphs A graph H = (W, F) is a subgraph of a graph G = (V, E) if W ⊆ V and F ⊆ E.

Subgraphs A graph H = (W, F) is a subgraph of a graph G = (V, E) if W ⊆ V and F ⊆ E.

Spanning Graphs A spanning graph of G is a subgraph of G that contains all vertices of G.

Spanning Graphs A spanning graph of G is a subgraph of G that contains all vertices of G.

Connectivity A graph G is connected if there is a path between any two vertices in G. The connected components of a graph are its maximal connected subgraphs.

Trees A tree is a graph that is connected and contains no cycles.

Spanning Trees A spanning tree of a graph is a spanning graph that is a tree.

Adjacency List Representation of a Graph List of vertices Pointer to head of adjacency list List of edges Pointers to adjacency list entries Adjacency list entry Pointer to edge Pointers to endpoints y d w u e c x a v b z w y u x z v d a b c e

Graph Exploration Goal: Visit all the vertices in the graph of G Count them Number them … Identify the connected components of G Compute a spanning tree of G

Graph Exploration ExploreGraph(G) 1 Label every vertex and edge of G as unexplored 2 for every vertex v of G 3 do if v is unexplored 4 then mark v as visited 5 S ← Adj(v) 6 while S is not empty 7 do remove an edge (u, w) from S 8 if (u, w) is unexplored 9 then if w is unexplored 10 then mark edge (u, w) as tree edge 11 mark vertex w as visited 12 S ← S ∪ Adj(w) 13 else mark edge (u, w) as a non-tree edge

Properties of ExploreGraph Theorem: Procedure ExploreGraph visits every vertex of G. Theorem: Every iteration of the for-loop that does not immediately terminate completely explores a connected component of G. Theorem: The graph defined by the tree edges is a spanning forest of G.

Connected Components ConnectedComponents(G) 1 Label every vertex and edge of G as unexplored 2 c ← 0 3 for every vertex v of G 4 do if v is unexplored 5 then c ← c + 1 6 assign component label c to v 7 S ← Adj(v) 8 while S is not empty 9 do remove an edge (u, w) from S 10 if (u, w) is unexplored 11 then if w is unexplored 12 then mark edge (u, w) as tree edge 13 assign component label c to w 14 S ← S ∪ Adj(w) 15 else mark edge (u, w) as a non-tree edge

Breadth-First Search Running time: O(n + m) BFS(G) 1 Label every vertex and edge of G as unexplored 2 for every vertex v of G 3 do if v is unexplored 4 then mark v as visited 5 S ← Adj(v) 6 while S is not empty 7 do (u, w) ← Dequeue(S) 8 if (u, w) is unexplored 9 then if w is unexplored 10 then mark edge (u, w) as tree edge 11 mark vertex w as visited 12 Enqueue(S, Adj(w)) 13 else mark edge (u, w) as a non-tree edge Running time: O(n + m)

Properties of Breadth-First Search Theorem: Breadth-first search visits the vertices of G by increasing distance from the root. Theorem: For every edge (v, w) in G such that v ∈ Li and w ∈ Lj, |i – j| ≤ 1. L0 L4 L3 L1 L2

Depth-First Search Running time: O(n + m) DFS(G) 1 Label every vertex and edge of G as unexplored 2 for every vertex v of G 3 do if v is unexplored 4 then mark v as visited 5 S ← Adj(v) 6 while S is not empty 7 do (u, w) ← Pop(S) 8 if (u, w) is unexplored 9 then if w is unexplored 10 then mark edge (u, w) as tree edge 11 mark vertex w as visited 12 Push(S, Adj(w)) 13 else mark edge (u, w) as a non-tree edge Running time: O(n + m)

An Important Property of Depth-First Search Theorem: For every non-tree edge (v, w) in a depth-first spanning tree of an undirected graph, v is an ancestor of w or vice versa.

A Recursive Depth-First Search Algorithm DFS(G) 1 Label every vertex and every edge of G as unexplored 2 for every vertex v of G 3 do if v is unexplored 4 then DFS(G, v) DFS(G, v) 1 mark vertex v as visited 2 for every edge (v, w) in Adj(v) 3 do if (v, w) is unexplored 4 then if w is unexplored 5 then mark edge (v, w) as tree edge 6 DFS(G, w) 7 else mark edge (v, w) as a non-tree edge