Graphs Graphs Simple graphs Algorithms Depth-first search - - PowerPoint PPT Presentation

graphs graphs
SMART_READER_LITE
LIVE PREVIEW

Graphs Graphs Simple graphs Algorithms Depth-first search - - PowerPoint PPT Presentation

CS171 Introduction to Computer Science II Graphs Graphs Simple graphs Algorithms Depth-first search Breadth-first search shortest path Connected components Directed graphs Weighted graphs Minimum spanning tree


slide-1
SLIDE 1

CS171 Introduction to Computer Science II

Graphs

slide-2
SLIDE 2

Graphs

  • Simple graphs
  • Algorithms

– Depth-first search – Breadth-first search – shortest path – Connected components

  • Directed graphs
  • Weighted graphs
  • Minimum spanning tree
  • Shortest path
slide-3
SLIDE 3
slide-4
SLIDE 4
slide-5
SLIDE 5
slide-6
SLIDE 6
slide-7
SLIDE 7
slide-8
SLIDE 8
slide-9
SLIDE 9
slide-10
SLIDE 10
slide-11
SLIDE 11
slide-12
SLIDE 12
slide-13
SLIDE 13

Edge-weighted graphs

  • Each connection has an associated weight
slide-14
SLIDE 14
slide-15
SLIDE 15
slide-16
SLIDE 16
slide-17
SLIDE 17
slide-18
SLIDE 18
slide-19
SLIDE 19
slide-20
SLIDE 20

Graphs

  • Simple graphs
  • Directed graphs
  • Weighted graphs
  • Shortest path in weighted digraphs
  • Symbol graphs
  • Project discussion
  • Minimum spanning trees
slide-21
SLIDE 21
slide-22
SLIDE 22
slide-23
SLIDE 23
slide-24
SLIDE 24

Dijkstra’s Algorithm

  • Finds all shortest paths given a source

for a graph with nonnegative edge weights

  • Can be used to solve single

destination problem

  • Intuition: grows the paths from the

source node using a greedy approach

slide-25
SLIDE 25

Shortest Paths – Dijkstra’s Algorithm

  • Assign to every node a distance value: set it to zero for

source node and to infinity for all other nodes.

  • Mark all nodes as unvisited. Set source node as current.
  • For current node, consider all its unvisited neighbors and

calculate their tentative distance. If this distance is less than the previously recorded distance, overwrite the distance (edge relaxation). Mark it as visited.

  • Set the unvisited node with the smallest distance from the

source node as the next "current node" and repeat the above

  • Done when all nodes are visited.
slide-26
SLIDE 26

Data structures

  • Shortest-paths-tree (SPT) is a subgraph containing

s as root and all vertices reachable from s and every path is a shortest path.

  • Two vertex-indexed arrays:

– Distance to the source: a vertex-indexed array distTo[] such that distTo[v] is the length of the shortest known path from s to v – Edges on the shortest paths tree: a parent-edge representation of a vertex-indexed array edgeTo[] where edgeTo[v] is the parent edge on the shortest path to v

slide-27
SLIDE 27

Dijkstra’s algorithm

slide-28
SLIDE 28
slide-29
SLIDE 29
slide-30
SLIDE 30
slide-31
SLIDE 31
slide-32
SLIDE 32

MapQuest

10 15 20

?

20 15 5 18 25 33

  • Shortest path for a single source-target pair
  • Dijkstra algorithm can be used
slide-33
SLIDE 33

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

slide-34
SLIDE 34
slide-35
SLIDE 35

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”

slide-36
SLIDE 36

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”

slide-37
SLIDE 37

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)

slide-38
SLIDE 38

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
slide-39
SLIDE 39

Admissible Heuristics

  • A heuristic is admissible if it is optimistic, estimating the

cost to be smaller than it actually is.

  • MapQuest:

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

slide-40
SLIDE 40
slide-41
SLIDE 41

Graphs

  • Simple graphs
  • Directed graphs
  • Weighted graphs
  • Shortest path
  • Symbol graphs
  • Project discussion
  • Minimum spanning trees
slide-42
SLIDE 42
slide-43
SLIDE 43
slide-44
SLIDE 44
slide-45
SLIDE 45

Applications

  • Phone/cable network design – minimum cost
  • Approximation algorithms for NP-hard

problems

slide-46
SLIDE 46
slide-47
SLIDE 47
slide-48
SLIDE 48
slide-49
SLIDE 49
slide-50
SLIDE 50
slide-51
SLIDE 51
slide-52
SLIDE 52
slide-53
SLIDE 53