Download presentation

Presentation is loading. Please wait.

Published byMargaret Chambers Modified over 2 years ago

1
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS

2
Graphs - Traversals Task: visit all nodes of a (connected) graph G, starting from a given node. Equivalent formulation of this task: Find a spanning tree T of graph G starting from a given node. Definition Spanning Tree T of G(V,E): T is a tree that contains all vertices of G: T(V T,E T ) V T =V ; E T < E.

3
4 Figure 1 4 Figure 2

4
Depth-First Search (DFS) 4 Continue exploration along a path as long as one can continue by adding edges one by one, until on reaches a node already discovered. Then, backtrack and start a new path. Breadth-First Search (BFS) 4 Starting from a node, go in all possible directions to nodes 1 edge away. Then, go one step from each of the newly discovered nodes and repeat until there are no new nodes to discover. Two Main Algorithms

5
4 Figure 3 (DFS diagram) 4 Figure 4 (BFS diagram)

6
4 Figure 5 The N-Queens Problem

7
4 Choose initial vertex (root of DFS tree). Label it O and push incident edges onto stack. Set counter c:=1; 4 Repeat until (stack empty). Take edge xy on top of stack and explore: if y has no label yet: assign it label c; increment c; push all incident edges yz on stack make xy a tree-edge else just pop xy off stack (a back-edge, going to ancestores, ends of tree-edges)

8
4 Invariant: Top stack vertex x always has a no. already. No back-edge can go to a non ancestor (else would have been explored earlier from another direction, would be a tree edge). O( |E| + |V| ) Because: o Each edge is stacked once at most in each direction o Each vertex requires a constant amount of processing. Analysis

9
4 Figure 6 Depth_First Search (DFS). Example:

10
Algorithm 4.2.1: Depth-First Search Input: a connected graph G and a starting vertex v Output: a depth-first spanning tree T and a standard vertex- labeling of G Initialize tree T as vertex v Initialize the set of frontier edges for tree T as empty Set dfnumber(v) = 0 Initialize label counter I := 1 While tree T does not yet span G Update the set of frontier edges for T. Let e be a frontier edge for T whose labeled

11
endpoints has the largest possible dfnumber. Let w be the unlabeled endpoint of edge e. Add edge e (and vertex w) to tree T. Set dfnumber(w) = i. i = i + 1. Return tree T with its dfnumbers. 4 Notation: For the standard vertex-labeling generated during the depth-first search, the integer-label assigned to a vertex w is denoted df number (w). Algorithm 4.2.1 contd

12
Input: a connected graph G and a starting vertex v Output: a depth-first spanning tree T and a standard vertex- labeling of G Initialize tree T as vertex v Initialize the set of frontier edges for tree T as empty Set dfnumber(v) = 0 Initialize label counter I := 1 While tree T does not yet span G Push onto the stack every frontier edges whose labeled endpoint has df number = i - 1. Pop the top edge from the stack and call it e Algorithm 4.2.2: Depth-First Search (Stack Implementation)

13
Algorithm 4.2.2 contd While edge e is not a frontier edge Pop next edge from the stack and call it e Let w be the unlabeled endpoint of e Add edge e (and vertex w) to tree T Set df number(w) = I I := I + 1 Return tree T with its dfnumbers.

14
Algorithm 7.3 DFS Skeleton Input: Array adjVertices of adjacency lists that represent a directed graph G = (V,E), as described in Section 7.2.3, and n, the number of vertices. The array is defined for indexes 1….n. Other parameters are as needed bye the application. Output: Return value depends on the application. Return type can vary; int is just an example Remarks: This skeleton is also adequate for some undirected graph problems that ignore nontree edges, but see algorithm 7.8. Color meanings are white = undiscovered, gray = active, black = finished.

15
Int dfsSweep(IntList[] adjVertices, int n, …) int ans; Allocate color arrya and initialize to white For each vertex v of G, in some order: if (color[v] == white) int vAns = dfs(adjVertices, color, v, …); (Process vAns) //Continue loop return ans;

16
Int dfs(IntList[] adjVertices, int[] color, int v, …) int w; intList remAdj; int ans; color[v] = gray; Preorder processing of vertix v remAdj = adjVertices[v]; while (remAdj != nil) w = first(remAdj); if (color[w] == white) Exploratory processing for tree edge vw

17
int wAns = dfs(adjVertices, color, w, …); Backtrack processing for tree edge vw, using wAns else Checking (I.e., processing ) for nontree edge vw remAdj = rest(remAdj) Postorder processing of vertex v, including final computation color[v] = black; return ans

18
Algorithm 4.2.3: Breadth-First Search Input: a connected graph G and a starting vertex v Output: a breadth-first spanning tree T and a standard vertex- labeling of G Initialize tree T as vertex v Initialize the set of frontier edges for tree T as empty Write label counter i : = 1 Initialize label counter i := 1 While tree T does not yet span G Update the set of frontier edges for T. Let e be a frontier edge for T whose labeled (contd)

19
endpoints has the largest possible dfnumber. Let w be the unlabeled endpoint of edge e. Add edge e (and vertex w) to tree T. Write label I on vertex w. i = i + 1. Return tree T with its dfnumbers. Algorithm 4.2.3 contd

20
Input: a connected graph G Output: a breadth-first spanning tree T and a standard vertex- labeling of G Initialize tree T as vertex v Initialize the set of frontier edges for tree T as empty Write label 0 on vertex v Initialize label counter i := 1 While tree T does not yet span G Enqueue (to the back of queue) every frontier edge whose labeled endpoint is = i - 1. Dequeue an edge from the front of queue and call it e Algorithm 4.2.4: Breadth-First Search (queue Implementation)

21
Algorithm 4.2.4 contd While edge e is not a frontier edge Dequeue next edge and call it e Let w be the unlabeled endpoint of e Add edge e (and vertex w) to tree T Write label I on vertex w i := i + 1 Return tree T with its dfnumbers. 4 Notation: Whereas the stack (Last-In-First-Out) is the appropriate data structure to store the frontier edges in a DFS, the queue (First-In-First-Out) is most appropriate for the BFS, since the frontier edges that arise earliest are given the highest priority.

22
Example 7.7 Breadth-first Search 4 Let’s see how breadth-first search works, starting from vertex A in the same graph as we used in Example 7.6. Instead of Terry the tourist, a busload of tourists start walking at A in the left diagram. They spread out and explore in all directions permitted by edges leaving A, looking for bargains. (We still think of edges as one-way bridges, but now they are one-way for walking, as well as traffic.) Figure 8

Similar presentations

OK

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.

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.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on nature and human interaction Ppt on regular expression syntax Ppt on as 14 amalgamation meaning Ppt on soft skills download Ppt on teamviewer 9 Ppt on motivational stories for students Ppt on classroom action research Ppt on power generation by speed breaker symbols Ppt on solar system for class 6 download Ppt on transient overvoltage in electrical distribution system