html5-img
1 / 22

GRAPHS AND GRAPH TRAVERSALS 9/26/2000

COMP 6/4030 ALGORITHMS. GRAPHS AND GRAPH TRAVERSALS 9/26/2000. 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

bree
Télécharger la présentation

GRAPHS AND GRAPH TRAVERSALS 9/26/2000

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 6/4030 ALGORITHMS GRAPHS AND GRAPH TRAVERSALS9/26/2000

  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(VT,ET) VT=V ; ET < E.

  3. Figure 1 • Figure 2

  4. Two Main Algorithms Depth-First Search (DFS) • 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) • 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.

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

  6. The N-Queens Problem • Figure 5

  7. Choose initial vertex (root of DFS tree). Label it O and push incident edges onto stack. Set counter c:=1; • 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. Analysis • 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.

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

  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. Algorithm 4.2.1 contd 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. • 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).

  12. Algorithm 4.2.2: Depth-First Search (Stack Implementation) 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

  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. Algorithm 4.2.3 contd 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.

  20. Algorithm 4.2.4: Breadth-First Search (queue Implementation) 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

  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. • 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 • 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

More Related