13
Chapter 9: Graphs Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

Embed Size (px)

Citation preview

Page 1: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

Chapter 9: GraphsChapter 9: Graphs

Breadth-First and Depth-First Search

Mark Allen Weiss: Data Structures and Algorithm Analysis in Java

Lydia Sinapova, Simpson College

Page 2: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

2

Breadth-First and Depth-First Search

BFS Basic Algorithm

BFS Complexity

DFS Algorithm

DFS Implementation

Relation between BFS and DFS

Page 3: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

3

BFS – Basic Idea

Given a graph with N vertices and a selected vertex A:

 for (i = 1;

there are unvisited vertices ; i++)

Visit all unvisited vertices at distance i

(i is the length of the shortest path between A and currently processed vertices)

Queue-based implementation

Page 4: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

4

BFS – Algorithm

BFS algorithm1. Store source vertex S in a queue and mark as

processed2. while queue is not empty Read vertex v from the queue for all neighbors w:

If w is not processedMark as processedAppend in the queueRecord the parent of w to be v (necessary

only if we need the shortest path tree)

Page 5: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

5

Example

1 4

2 3 5

6

Adjacency lists

1: 2, 3, 42: 1, 3, 63: 1, 2, 4, 5, 64: 1, 3, 55: 3, 46: 2, 3

Breadth-first traversal: 1, 2, 3, 4, 6, 5

1: starting node

2, 3, 4 : adjacent to 1

(at distance 1 from node 1)

6 : unvisited adjacent to node 2.

5 : unvisited, adjacent to node 3

The order depends on the order of

the nodes in the adjacency lists

Page 6: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

6

Shortest Path Tree

Consider the distance table:

NodesA B C D E

Distance to A 0 1 1 2 1

Parent 0 A A C A

The table defines the shortest path tree, rooted at A.

Page 7: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

7

BFS – Complexity

Step 1 : read a node from the queue O(V) times.

Step 2 : examine all neighbors, i.e. we examine all edges of the currently read node.

Not oriented graph: 2*E edges to examine

Hence the complexity of BFS is O(V + 2*E)

Page 8: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

8

Depth-First Search

Procedure dfs(s)mark all vertices in the graph as not reachedinvoke scan(s)

Procedure scan(s)mark and visit sfor each neighbor w of s

if the neighbor is not reachedinvoke scan(w)

Page 9: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

9

Depth-First Search with Stack

Initialization: mark all vertices as unvisited, visit(s)

while the stack is not empty: pop (v,w) if w is not visited

add (v,w) to tree T visit(w)

Procedure visit(v) mark v as visited for each edge (v,w)

push (v,w) in the stack

Page 10: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

10

Recursive DFS

DepthFirst(Vertex v)

visit(v);

for each neighbor w of v

if (w is not visited)

add edge (v,w) to tree T

DepthFirst(w)

Visit(v)mark v as visited

Page 11: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

11

Example

1 4

2 3 5

6

Adjacency lists

1: 2, 3, 42: 6, 3, 13: 1, 2, 6, 5, 44: 1, 3, 55: 3, 46: 2, 3

Depth first traversal: 1, 2, 6, 3, 5, 4the particular order is dependent on the order of nodes in the adjacency lists

Page 12: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

12

BFS and DFSbfs(G)

list L = empty

tree T = empty

choose a starting vertex x

visit(x)

while(L nonempty)

remove edge (v,w) from

beginning of L

if w not visited

add (v,w) to T

visit(w)

dfs(G)

list L = empty

tree T = empty

choose a starting vertex x

visit(x)

while(L nonempty)

remove edge (v,w) from

end of L

if w not visited

add (v,w) to T

visit(w)

Visit ( vertex v)

mark v as visited

for each edge (v,w)

add edge (v,w) to end of L

Page 13: Chapter 9: Graphs Breadth-First and Depth-First Search Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College

13

Applications of DFS

Trees: preorder traversal Graphs:

Connectivity Biconnectivity – articulation points Euler graphs