- 169 Views
- Updated On :
- Presentation posted in: General

Graphs-2. 1 April 2003. Depth-First Search. Depth-first traversal of a graph is roughly analogous to preorder traversal of an ordered tree. Suppose that the traversal has just visited a vertex v, and let w 1, w 2 , … , w k be the vertices adjacent to v.

Graphs-2

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Graphs-2

1 April 2003

- Depth-first traversal of a graph is roughly analogous to preorder traversal of an ordered tree.
- Suppose that the traversal has just visited a vertex v, and let w1, w2, … , wk be the vertices adjacent to v.
- Then we shall next visit w1 and keep w2, … , wk waiting.
- After visiting w1, we traverse all the vertices to which it is adjacent before returning to traverse w2, … , wk.

http://www.cs.duke.edu/csed/jawaa/DFSanim.html

initialize mark[1], mark[2], …, mark[n] to unvisited

foreach vertex v from 1 to n do

if mark[v] = unvisited thendepth-first-search(v)

proceduredepth-first-search(v : vertex);

set mark[v] to visited

for each vertex w adjacent to v do

if mark[w] = unvisited then

depth-first-search(w)

enddepth-first-search

f

b

a

e

g

d

c

dfs(a); mark a, dfs(b); mark b, visit either c or d, dfs(c); mark c, a has been visited so go back to c and b. Now dfs(d); mark d, and note that a and c are marked; go back to b and then return to a; dfs is now finished.

- Assume adjacency list representation
- If e is the number of arcs and n e then the number of calls will take O(e) time.
- This is because no vertex is visited more than once due to mark[v] being appropriately set and tested.
- Therefore the total time is proportional to the sum of the sizes of the adjacency lists.
- This is within a constant factor of the time necessary to “look at” each arc.

Start

0

1

2

8

4

3

5

6

7

Depth-first traversal

- There are problems where we can say that one task has to be done before another, or depends on the other, but can't easily see what order to do things.
- For example, it is easy to specify/look up prerequisite relationships between courses in a program, but it may be hard to find an order to take all the courses so that all prerequisite material is covered before the courses that depend on them.

- The same problem arises in spreadsheets. To recalculate cells after some of the values change, it is necessary to consider how one cell’s value depends on the value in others.
Cell Contents Value

1 (Cell 2) * 2 200

2 100 100

3(Cell 2) + 10 110

4 (Cell 3) + (Cell 1) 310

- In this example, if you change the value in cell 2, you need to first recalculate the values in cell 3 and 1, and only when that is done can you recalculate the value in cell 4.

- A straightforward approach to finding this order would be first to find the nodes that each node depends on
Node Depends on

2 none

3 2

1 2

4 3,1

- You would first remove any node that has no dependencies, as an acceptable first node. That node could then be removed from the dependency lists of the other nodes:
Node 2 is removed

Node Depends on

3 none

1 none

4 3,1

- The process then repeats, with either node 3 or 1 being chosen. Let’s pick node 3.
Node Order: 2, 3

Node Depends on

1 none

4 1

- This continues in the obvious manner, with the final order being 2,3,1,4.
- In fact, to implement this we don't even need to store lists of nodes that a node depends on, just the NUMBER of such nodes.
- You start by finding the number of such nodes, for each node. Then when a node n (with zero nodes depending on it) is removed, you decrement the number associated with each of the nodes m depending on it (precisely those with an edge from node m to n).

list L empty

while (G is not empty)

find a vertex v with no incoming edges

delete v and its adjacent edges from G

add v to L

To implement this, we can use a list datastructure L to return the topologically sorted nodes, and a queue data structure Q to hold the nodes with zero dependent nodes waiting to be processed.

When a node N is removed from Q and added to L, all of N’s dependent neighbors have their number decremented, and any that become zero are added to Q.

Example:

QL

2 2

3 1 2 3

1 2 3 1

42 3 1 4

- Both problems are essentially equivalent.
- The data of both can be represented in a directed graph.
- In the first each node is a course
- In the second example each node is a spreadsheet cell.

- Directed edges occur when one node depends on the other, because of prerequisite relationships among courses or dependencies among nodes.
- The problem in both is to find an acceptable ordering of the nodes satisfying the dependencies.

- Let G be a directed graph with no cycles. A topological order for G is a sequential listing of all the vertices in G such that, for all vertices v, w G, if there is an edge from v to w, then v precedes w in the sequential listing.

initialize mark[1], mark[2], …, mark[n] to unvisited

foreach vertex v from 9 to 1 do

if mark[v] = unvisited thentopological-sort(v)

proceduretopological-sort(v : vertex);

push (v,S)

set mark[v] to visited

for each vertex w adjacent to v do

if mark[w] = unvisited then

topological-sort(w)

output (top(S))

pop (S)

endtopological-sort

0

1

2

3

4

5

6

7

8

9

3

6

0

1

5

9

4

8

2

7

- Breadth-first traversal of a graph is roughly analogous to a level-by-level traversal of an ordered tree. If the traversal has just visited a vertex v, then it next visits all the vertices adjacent to v, putting the vertices adjacent to these in a waiting list to be traversed after all vertices adjacent to v have been visited.

Start

0

1

4

3

2

6

5

7

8

Breadth-first traversal

http://al.ei.tuat.ac.jp/~sekisita/graph2-e.html