traversing the web - wellesley collegecs.wellesley.edu/~cs230/slides/14-bfstopologicalsort... ·...

7
Reading LDC 19.5 1 § Strongly Connected: A graph for which there is a directed path from any node to any other node § Is this graph strongly connected? § Strongly connected component: A strongly connected sub-graph § Can you find the strongly connected components of this graph? 2

Upload: others

Post on 10-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Traversing the Web - Wellesley Collegecs.wellesley.edu/~cs230/slides/14-BFSTopologicalSort... · 2019-03-27 · Topological Sorting Algorithm that has no successor • Remove v from

Reading LDC 19.5

1

§ Strongly Connected: A graph for which there is a directed path from any node to any other node

§ Is this graph strongly connected?

§ Strongly connected component:A strongly connected sub-graph

§ Can you find the strongly connected components of this graph?

2

Page 2: Traversing the Web - Wellesley Collegecs.wellesley.edu/~cs230/slides/14-BFSTopologicalSort... · 2019-03-27 · Topological Sorting Algorithm that has no successor • Remove v from

• Directed Graph of Nodes and Arcs• Nodes = web pages• Arcs = hyperlinks from a page to another

• A graph can be explored• A graph can be indexed

Server and domainAccess method

URL

DocumentPath

http://cs.wellesley.edu/~cs230/PPTs/Hash.html

3

Traversing the Web

• The web can be considered a graph “the web graph”

• Web pages are the graph nodes

• Hyperlinks on pages are graph arcs

• The web graph is huge (way over one million billions nodes) - maybe infinite (pages are created on the fly)

• For traversing the web graph, DFS is not a good strategy. (Why?)

• You need to explore your neighborhood before going deeper

The shape of the Web is …

a “bow-tie”(!)

4

Page 3: Traversing the Web - Wellesley Collegecs.wellesley.edu/~cs230/slides/14-BFSTopologicalSort... · 2019-03-27 · Topological Sorting Algorithm that has no successor • Remove v from

1-5

21

5

89

7

4 3

6

Queue: Iterator:

9 6 7 8 43 5 1 2

Breadth First Example: BFS(9)

5

// BFS traversal starting at v// Initialization:Mark all vertices as unvisitedenQueue v onto a new queue QMark v as visited// Procedure:While (Q is not empty)

deQueue a vertex w from QFor each unvisited vertex u adjacent to w:

enQueue u onto QMark u as visited

ia

b

c

h

eGS

f

BFS from S to G:

S

a

i

c

b e G

h

fThe BFS tree shows the visitsHow do you remember the path?

BFS(v) pseudocode

6

Page 4: Traversing the Web - Wellesley Collegecs.wellesley.edu/~cs230/slides/14-BFSTopologicalSort... · 2019-03-27 · Topological Sorting Algorithm that has no successor • Remove v from

S

Sa Sh

Sh Sai Sab

Sai Sab She ShG

Sab She ShG

She ShG Sabc

ShG Sabc

… in the next step it will reach the goal G

Initialization: enqueue path [S] in QWhile you have not reached G

dequeue a path from BFS queue and check the last node x in the pathextend the path to unvisited neighbors of xand enqueue extended paths to back of Q

S [S]

a [Sa]

i [Sai]

c [Sabc]

b [Sab] e [She] G [ShG]

h [Sh]

f [ShGf]

ia b

c

h

e GS

f

How do you remember the path?

7

8

Page 5: Traversing the Web - Wellesley Collegecs.wellesley.edu/~cs230/slides/14-BFSTopologicalSort... · 2019-03-27 · Topological Sorting Algorithm that has no successor • Remove v from

• Defined on a Directed Acyclic Graph (a “DAG”)• Usually reflect dependencies or requirements

• I.e., Assembly lines, Supply lines, Organizational charts, …• BTW: You cannot take 231 after 230 unless…

• Understanding dependencies requires “topological sorting”

112

204

115

232

111

230

231

242

240

315

225

251

235

Dependency Graph on a DAG

9

• Topological order– A list of vertices in a DAG such that

vertex x precedes vertex y iffthere is a directed edge from x to y in the graph

– There may be several topological orders in a given graph• Topological sorting

– Arranging the vertices into a topological order

a b c

d e f

g

Resolving DAG Dependencies

10

Page 6: Traversing the Web - Wellesley Collegecs.wellesley.edu/~cs230/slides/14-BFSTopologicalSort... · 2019-03-27 · Topological Sorting Algorithm that has no successor • Remove v from

c

f

a

g

d e

b• Select a vertex vthat has no predecessor

• Remove v from the graph (along with all associated arcs),

• Add v to the end of a list of vertices L

• Repeat previous steps

• When the graph is empty, L’s vertices will be in topological order

fcebda g

Topological Sorting Algorithm

11

• Select a vertex vthat has no successor

• Remove v from the graph (along with all associated arcs),

• Add v to the beginning of a list of vertices L

• Repeat previous steps

• When the graph is empty, L’s vertices will be in topological order

a

g

b

d f

c

e

fcebdga

A(nother) Topological Sorting Algorithm

12

Page 7: Traversing the Web - Wellesley Collegecs.wellesley.edu/~cs230/slides/14-BFSTopologicalSort... · 2019-03-27 · Topological Sorting Algorithm that has no successor • Remove v from

Assuming you began at node a, give the order of traversal if you visited every node.

For DFS:For BFS:

a b

c

d e f

g

h

i

13

Give two different possible topological sorts of this graph:

a b

c

d e f

g

h

i

14