Download presentation

Presentation is loading. Please wait.

Published byMiles Lucas Dorsey Modified over 2 years ago

1
zhengjin 1 2015-5-9 Graphs: Adjacency Matrix ● Example: 1 24 3 a d bc A1234 1 2 3 ?? 4

2
zhengjin 2 2015-5-9 Breadth-First Search ● “Explore” a graph, turning it into a tree ■ One vertex at a time ■ Expand frontier of explored vertices across the breadth of the frontier ● Builds a tree over the graph ■ Pick a source vertex to be the root ■ Find (“discover”) its children, then their children, etc.

3
zhengjin 3 2015-5-9 Breadth-First Search ● Again will associate vertex “colors” to guide the algorithm ■ White vertices have not been discovered ○ All vertices start out white ■ Grey vertices are discovered but not fully explored ○ They may be adjacent to white vertices ■ Black vertices are discovered and fully explored ○ They are adjacent only to black and gray vertices ● Explore vertices by scanning adjacency list of grey vertices

4
zhengjin 4 2015-5-9 Breadth-First Search BFS(G, s) { initialize vertices; Q = {s};// Q is a queue (duh); initialize to s while (Q not empty) { u = RemoveTop(Q); for each v u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } What does v->p represent? What does v->d represent?

5
zhengjin 5 2015-5-9 Breadth-First Search: Example rstu vwxy

6
zhengjin 6 2015-5-9 Breadth-First Search: Example 0 rstu vwxy s Q:

7
zhengjin 7 2015-5-9 Breadth-First Search: Example 1 0 1 rstu vwxy w Q: r

8
zhengjin 8 2015-5-9 Breadth-First Search: Example 1 0 1 2 2 rstu vwxy r Q: tx

9
zhengjin 9 2015-5-9 Breadth-First Search: Example 1 2 0 1 2 2 rstu vwxy Q: txv

10
zhengjin 10 2015-5-9 Breadth-First Search: Example 1 2 0 1 2 2 3 rstu vwxy Q: xvu

11
zhengjin 11 2015-5-9 Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: vuy

12
zhengjin 12 2015-5-9 Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: uy

13
zhengjin 13 2015-5-9 Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: y

14
zhengjin 14 2015-5-9 Breadth-First Search: Example 1 2 0 1 2 2 3 3 rstu vwxy Q: Ø

15
zhengjin 15 2015-5-9 BFS: The Code Again BFS(G, s) { initialize vertices; Q = {s}; while (Q not empty) { u = RemoveTop(Q); for each v u->adj { if (v->color == WHITE) v->color = GREY; v->d = u->d + 1; v->p = u; Enqueue(Q, v); } u->color = BLACK; } What will be the running time? Touch every vertex: O(V) u = every vertex, but only once (Why?) So v = every vertex that appears in some other vert’s adjacency list Total running time: O(V+E)

16
zhengjin 16 2015-5-9 Breadth-First Search: Properties ● BFS calculates the shortest-path distance to the source node ■ Shortest-path distance (s,v) = minimum number of edges from s to v, or if v not reachable from s ● BFS builds breadth-first tree, in which paths to root represent shortest paths in G ■ Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time

Similar presentations

OK

Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E| |V| 2 ; Sparse graph: |E|

Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E| |V| 2 ; Sparse graph: |E|

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on brain tumor Ppt on soft skills for engineering students Ppt on business plan of event management Financial management books free download ppt on pollution Download ppt on adolescence and puberty Ppt on bluetooth architecture Ppt on marketing management by philip kotler video Ppt on personality development for school students Download ppt on unification of italy Ppt on wireless communication