Graph Traversal Chapter 9 Used lecture slides of

  • Slides: 34
Download presentation
Graph Traversal Chapter 9 Used lecture slides of Skiena

Graph Traversal Chapter 9 Used lecture slides of Skiena

Graphs

Graphs

Types of Graphs

Types of Graphs

Types of Graphs

Types of Graphs

Data Structures for Graphs

Data Structures for Graphs

Data Structures for Graphs

Data Structures for Graphs

Data Structures for Graphs • Table of Edges - An even simpler data structure

Data Structures for Graphs • Table of Edges - An even simpler data structure is just to maintain an array or linked list of edges. This is not as flexible as the other data structures at answering “who is adjacent to vertex x”?

Directed and Undirected Edges

Directed and Undirected Edges

Breadth-First Traversal

Breadth-First Traversal

Discovered vs. Processed Vertices

Discovered vs. Processed Vertices

Initializing Search

Initializing Search

BFS Implementation

BFS Implementation

Exploiting Traversal

Exploiting Traversal

Finding Paths

Finding Paths

Finding Paths

Finding Paths

Depth-First Search (DFS)

Depth-First Search (DFS)

DFS Implementation

DFS Implementation

Connected Components

Connected Components

Connected Components

Connected Components

Depth-First Search (DFS)

Depth-First Search (DFS)

Previsit and Postvisit Orderings – Each pre and post visit is an ordered event

Previsit and Postvisit Orderings – Each pre and post visit is an ordered event Account for all edges – Tree edges and Back edges

Previsit and Postvisit Orderings – Each pre and post visit is an ordered event

Previsit and Postvisit Orderings – Each pre and post visit is an ordered event

Previsit and Postvisit Orders • DFS yields a forest (disjoint trees) when there are

Previsit and Postvisit Orders • DFS yields a forest (disjoint trees) when there are disjoint components in the graph • Can use pre/post visit numbers to detect properties of graphs • Account for all edges: Tree edges (solid) and back edges (dashed) • Back edges detect cycles • Properties still there even if explored in a different order (i. e. start with F, then J)

Depth-First Search in Directed Graphs • Tree edges and back edges (2 below) the

Depth-First Search in Directed Graphs • Tree edges and back edges (2 below) the same as before l Added terminology for DFS Tree of a directed graph – Forward edges lead from a node to a nonchild descendant (2 below) – Cross edges lead to neither descendant or ancestor, lead to a node which has already had its postvisit (2 below)

Back Edge Detection • Ancestor/descendant relations, as well as edge types can be read

Back Edge Detection • Ancestor/descendant relations, as well as edge types can be read off directly from pre and post numbers of the DFS tree – just check nodes connected by each edge • Initial node of edge is u and final node is v • Tree/forward reads pre(u) < pre(v) < post(u)

Topological Sorting

Topological Sorting

Longest Path in a DAG

Longest Path in a DAG

Topological Sorting Algorithm

Topological Sorting Algorithm

Strongly Connected Components l Two nodes u and v in a directed graph are

Strongly Connected Components l Two nodes u and v in a directed graph are connected if there is a path from u to v and a path from v to u l The disjoint subsets of a directed graph which are connected are called strongly connected components l How could we make the entire graph strongly connected?

Meta-Graphs • Every directed graph is a DAG of its strongly connected components This

Meta-Graphs • Every directed graph is a DAG of its strongly connected components This meta-graph decomposition will be very useful in many applications (e. g. a high level flow of the task with subtasks abstracted)

Example • Create GR by reversing graph G • Do DFS on GR •

Example • Create GR by reversing graph G • Do DFS on GR • Repeat until graph G is empty: G – Pick node with highest remaining post score (from DFS tree on GR), and explore starting from that node in G – That will discover one sink SCC in G – Prune all nodes in that SCC from G and GR • Complexity? – Create GR – Post-ordered list –add nodes as do dfs(GR ) – Do DFS with V reverse ordered from PO list • visited flag = removed GR

Discovering Separating Vertices with DFS l a is a separating vertex of G if

Discovering Separating Vertices with DFS l a is a separating vertex of G if and only if either l a is the root of the DFS tree and has more than one child, or l a is not the root, and there exists a child s of a such that there is no back edge between any descendent of s (including s) and a proper ancestor of a. DFS tree with pre-ordering a: 1 a b c b: 2 c: 3 d d: 4 e e: 5

l a is a separating vertex of G if and only if either l

l a is a separating vertex of G if and only if either l a is the root of the DFS tree and has more than one child, or l a is not the root, and there exists a child s of a such that there is no backedge between any descendent of s (including s) and a proper ancestor of a. DFS tree with pre-ordering and low numbering a: 1, 1 a b f c e b: 2, 2 c: 3, 1 d u is a sep. vertex iff there is any child v of u such that low(v) ≥ pre(u) f: 6, 6 d: 4, 1 e: 5, 1

Example with pre and low a a, 1, 1 f b, 2, 1 g

Example with pre and low a a, 1, 1 f b, 2, 1 g b low numbering g, 3, 1 e c d c, 4, 3 e, 7, 1 d, 5, 3 f, 8, 1 h h, 6, 4 u is a sep. vertex iff there is any child v of u s. t. low(v) ≥ pre(u)