YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: CS171 Introduction to Computer Science II

CS171 Introduction to Computer Science II

Graphs

Page 2: CS171 Introduction to Computer Science II

Announcements/Reminders

• Median grades of midterms – Midterm 2: 88 – Midterm 1: 90

• Median grades of quizzes – Quiz 3: 91 – Quiz 2: 81 – Quiz 1: 89

• Quiz 4 on Friday (priority queue, graphs) • Project demo due Sunday night (optional) • Project workshop on Monday

Page 3: CS171 Introduction to Computer Science II

Graphs

• Simple graphs • Directed graphs • Weighted graphs • Shortest path in weighted digraphs

– Dijkstra Algorithm – Implementation – A* Algorithm

• Project discussion • Symbol graphs • Minimum spanning trees

Page 4: CS171 Introduction to Computer Science II
Page 5: CS171 Introduction to Computer Science II

Shortest Paths – Dijkstra’s Algorithm

• Assign a distance value to every node – zero for source node – infinity for all other nodes.

• Add source node to a queue (open set) • While the queue is not empty

– Remove the node with the smallest distance from the source node as the "current node”, mark it as visited (visited set)

– For current node, consider all its unvisited neighbors and calculate their tentative distance. Add/update them in the queue: if the distance is less than the previously recorded distance, overwrite the distance (edge relaxation)

Page 6: CS171 Introduction to Computer Science II
Page 7: CS171 Introduction to Computer Science II
Page 8: CS171 Introduction to Computer Science II

Dijkstra’s algorithm

Page 9: CS171 Introduction to Computer Science II
Page 10: CS171 Introduction to Computer Science II
Page 11: CS171 Introduction to Computer Science II

MapQuest

10

15

20 ?

20

15

5

18

25

33

• Shortest path for a single source-target pair

• Dijkstra algorithm can be used

Page 12: CS171 Introduction to Computer Science II

Better Solution: Make a ‘hunch”!

• Use heuristics to guide the search

– Heuristic: estimation or “hunch” of how to search for a solution

• We define a heuristic function:

h(n) = “estimate of the cost of the cheapest path from the starting node to the goal node”

Page 13: CS171 Introduction to Computer Science II

The A* Search

• A* is an algorithm that:

– Uses heuristic to guide search

– While ensuring that it will compute a path with minimum cost

• A* computes the function f(n) = g(n) + h(n)

“actual cost”

“estimated cost”

Page 14: CS171 Introduction to Computer Science II

A*

• f(n) = g(n) + h(n)

– g(n) = “cost from the starting node to reach n”

– h(n) = “estimate of the cost of the cheapest path from n to the goal node”

10

15

20

20

15

5

18

25

33

n g(n)

h(n)

Page 15: CS171 Introduction to Computer Science II

Properties of A*

• A* generates an optimal solution if h(n) is an admissible heuristic and the search space is a tree:

– h(n) is admissible if it never overestimates the cost to reach the destination node

• A* generates an optimal solution if h(n) is a consistent heuristic and the search space is a graph:

– h(n) is consistent if for every node n and for every successor node n’ of n:

h(n) ≤ c(n,n’) + h(n’)

n

n’

d

h(n)

c(n,n’) h(n’)

• If h(n) is consistent then h(n) is admissible • Frequently when h(n) is admissible, it is also consistent

Page 16: CS171 Introduction to Computer Science II

Admissible Heuristics

• A heuristic is admissible if it is optimistic, estimating the cost to be smaller than it actually is.

• MapQuest:

h(n) = “Earth distance to destination” is admissible as normally cities are not connected by roads that make straight lines

Page 17: CS171 Introduction to Computer Science II

Shortest Paths – A* Algorithm

• Assign a distance value to every node – zero for source node – infinity for all other nodes.

• Add source node to a queue (open set) • While the queue is not empty

– Remove the node with the smallest estimated cost from the source node to the target node as the "current node”, mark it as visited (visited set)

– For current node, consider all its unvisited neighbors and calculate their tentative distance. Add/update them in the queue: if the distance is less than the previously recorded distance, overwrite the distance (edge relaxation)

Page 18: CS171 Introduction to Computer Science II

Project

• Option 1: MapQuest – Weighted digraph – Shortest path (Dijkstra) – Shortest path (A* for bonus points)

• Option 2: FaceSpace – Digraph – Search by user profiles – Add profiles – Remove profiles – Add friendships – Shortest path (BFS)

Page 19: CS171 Introduction to Computer Science II

Symbol Graph

• Typical applications involve graphs using strings, not integer indices, to define and refer to vertexes

– User name in FaceSpace

– City name for MapQuest

Page 20: CS171 Introduction to Computer Science II
Page 21: CS171 Introduction to Computer Science II
Page 22: CS171 Introduction to Computer Science II

Graphs

• Simple graphs

• Directed graphs

• Weighted graphs

• Shortest path

• Symbol graphs

• Project discussion

• Minimum spanning trees

Page 23: CS171 Introduction to Computer Science II
Page 24: CS171 Introduction to Computer Science II
Page 25: CS171 Introduction to Computer Science II
Page 26: CS171 Introduction to Computer Science II

Applications

• Phone/cable network design – minimum cost

• Approximation algorithms for NP-hard problems

Page 27: CS171 Introduction to Computer Science II

Assumptions

• The graph is connected

• If the graph is not connected: find minimum spanning trees of each connected components, i.e. minimum spanning forest

Page 28: CS171 Introduction to Computer Science II
Page 29: CS171 Introduction to Computer Science II
Page 30: CS171 Introduction to Computer Science II
Page 31: CS171 Introduction to Computer Science II
Page 32: CS171 Introduction to Computer Science II
Page 33: CS171 Introduction to Computer Science II
Page 34: CS171 Introduction to Computer Science II
Page 35: CS171 Introduction to Computer Science II

Quiz 4


Related Documents