13
Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

Embed Size (px)

Citation preview

Page 1: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

Breadth-First Search

Seminar – Networking AlgorithmsCS and EE Dept.Lulea University of Technology

27 Jan. 2005

Mohammad Reza Akhavan

Page 2: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

Outline (BFS)

Rooted Tree Spanning Tree Breadth-First Search BFS Algorithm Example

Page 3: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

Rooted Tree

A rooted tree is a tree in which a special node is singled out. This node is called the "root“. A tree which is not rooted is sometimes called a free tree.

Rooted Tree Free Tree

Page 4: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

Spanning Tree

A spanning tree in a graph G with n nodes and m edges:

A sub-graph that connects all the nodes. A sub-graph with no cycles. A sub-graph with m=n-1 edges.

Page 5: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

Breadth-First Search

Visits all the nodes and edges of G Determines whether G is connected Computes the connected components of G Find and report a path with the minimum number

of edges between two given nodes Find a simple cycle, if there is one Provide the shortest path from a given root to

all other nodes of the network

Page 6: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

BFS for Shortest Path (i=0)

0s

The algorithm uses a mechanism for setting and getting “labels” of nodes and edges. Nodes whose distance from s is 0 are labeled.

Page 7: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

BFS for Shortest Path (i=1)

0

1

1

1s

Nodes whose distance from s is 1 are labeled.

Page 8: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

BFS for Shortest Path (i=2)

0

1

1

2

2

1

2

s

Nodes whose distance from s is 2 are labeled.

Page 9: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

BFS for Shortest Path (i=3)

0

1

1

2

2

1

2

s

3

Nodes whose distance from s is 3 are labeled.

The next iteration finds out that the whole graph is labeled and therefore the procedure stops.

Page 10: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

The BFS Tree

0

1

1

2

2

1

2

s

3

0

1

1

2

2

1

2

s

3

Page 11: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

BFS Algorithm

The algorithm uses a mechanism for setting and getting “labels” of nodes and edges

Search a graph by increasing distance from the starting vertex (or from the starting vertices in case of several connected components).

Can think of creating one level after the other (by increasing depth).

Page 12: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

BFS – Asynchronies Mode

332

0

4

1

1

1

4 2 22

3 44

32

7

2

5

37

6

4

3

4

5

4

3

76

Page 13: Breadth-First Search Seminar – Networking Algorithms CS and EE Dept. Lulea University of Technology 27 Jan. 2005 Mohammad Reza Akhavan

References:

Introduction to Distributed Algorithms

by Gerard Tel.

Google !