section 9 graph search algorithms. breadth-first search idea: let |n| denote a distance of node n...

22
Section 9 • Graph search algorithms

Upload: sheila-bond

Post on 02-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

Section 9

• Graph search algorithms

Page 2: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

Breadth-first search

• Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order:

• All nodes n such that |n| = 1

• All nodes n such that |n| = 2

• All nodes n such that |n| = 3

• All nodes n such that |n| = 4

• All nodes n such that |n| = 5

....

Page 3: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

BFS: Water analogy

• BFS is similar to pouring water!

• We pour the water on the first node. When there is too much water in it, the nodes adjacent to it start to fill. And so on...

Page 4: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

BFS

Page 5: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

Depth-first search

• Idea: We go as deep as possible.

• Visit recursively all the adjacent nodes of the source node.

Page 6: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

DFS: Labyrinth analogy

• DFS is similar to going through labyrinth.

• We walk leaving a thread behind us. Whenever we have the choice of path, we choose the leftmost one. If we reach a dead-end or the place already marked, we go back to the first unvisited place.

Page 7: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

DFS

Page 8: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

BFS + DFS

• Stunning fact - we can implement both of them using the same code, changing only the underlying SequenceStructure.

Page 9: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

BFS + DFS

• initialize toDo with the node we are starting from

while (!empty(toDo))

{

remove a node n from toDo

visit(n)

put all unvisited neighbours of

n on toDo

}

Page 10: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

BFS + DFS

Method toDo implementation

DFS Stack

BFS Queue

Best-first search Priority queue

Page 11: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

Best-first search

• In situation when we have a weight function f on nodes, which tells us which node to visit first.

• BFS, but visiting neighbours according to the fumction f.

• Just use priority queue!

Page 12: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

WWW as a graph

• Nodes -

• Edges -

Page 13: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

WWW as a graph

• Nodes - webpages

• Edges - links

Page 14: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

WWW as a graph

• How to write a program downloading a whole web page?

Page 15: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

WWW as a graph

• How to write a program downloading a whole web page?

• Use graph search!

• Which one?

Page 16: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

WWW as a graph

• How to write a program downloading a whole web page?

• Use graph search!

• Which one?

• BFS!

Page 17: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

WWW as a graph

• Interesting questions:

• What is the diameter of the graph? (probably about 20)

• What is its structure?

• What are the efficient search algorithms? - Google, Altavista

• What is a „typical” node?

• Many more...

Page 18: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

Bus/train map as a graph

• Nodes -

• Edges -

Page 19: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

Bus map as a graph

• Nodes - bus/train stops/stations

• Edges - there’s an edge from one node to the other if there is a bus/train, which takes you from one stop to the other.

• www.bahn.de

• How to find how to reach a bus stop/city?

Page 20: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

Survival skills

• How to find a way out in the labyrinth?

• Optimistic version: With a thread of string.

• Pessimistic version: Without a thread.

Page 21: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

With a thread

• Use DFS - using a thread to go back to the yet unvisited paths.

Page 22: Section 9 Graph search algorithms. Breadth-first search Idea: Let |n| denote a distance of node n from the initial node. We visit nodes in order: All

Without a thread

• A heuristics which often works - always turn left.