cs171 introduction to computer science ii graphs strike back

Download CS171 Introduction to Computer Science II Graphs Strike Back

If you can't read please download the document

Upload: colin-daniel

Post on 16-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1
  • CS171 Introduction to Computer Science II Graphs Strike Back
  • Slide 2
  • Homework 5! Emory Movie Database (EMD) Due Wednesday 4/7 11:59pm!
  • Slide 3
  • Depth-First Search (DFS) Non-recursive algorithm Push s onto a stack Repeat until the stack is empty: remove the top vertex v. if not visited, mark as visited add all vs unvisited neighbors to the stack
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • DFS in real-life (xkcd)
  • Slide 9
  • Graphs Definitions Implementation/Representation of graphs Undirected graphs Algorithms Depth-first search Breadth-first search Path finding and shortest path Connected components Applications
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • BFS example
  • Slide 14
  • Graphs Definitions Implementation/Representation of graphs Undirected graphs Algorithms Depth-first search Breadth-first search Path finding and shortest path Connected components Applications
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Graphs Definitions Implementation/Representation of graphs Undirected graphs Algorithms Depth-first search Breadth-first search Path finding and shortest path Connected components Applications
  • Slide 24
  • Slide 25
  • Six degrees of separation Everyone is on average approximately six steps away, by way of introduction, from any other person on Earth Online social networks Facebook: average distance is 4.74 (Nov 2011) Twitter: average distance is 4.67 Erdos number Bacon number
  • Slide 26
  • Slide 27
  • Slide 28
  • Application: Web Search Engines i.Gather the contents of all web pages (using a program called a crawler or spider) ii.Organize the contents of the pages in a way that allows efficient retrieval (indexing) iii. Take in a query, determine which pages match, and show the results (ranking and display of results) A Search Engine does three main things:
  • Slide 29
  • Basic structure of a search engine: Crawler disks Index indexing hooli.com Query: computer look up
  • Slide 30
  • Breadth-First Crawl: Basic idea: - start at a set of known URLs - explore in concentric circles around these URLs start pages distance-one pages distance-two pages
  • Slide 31
  • Graphs Definitions Implementation and representation of graphs Simple graphs Depth-first search Breadth-first search shortest path Connected components Directed graphs Weighted graphs Shortest path Minimum Spanning Tree
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
  • Slide 38
  • Slide 39
  • Slide 40
  • Slide 41
  • Slide 42
  • Edge-weighted graphs Each connection has an associated weight
  • Slide 43
  • Slide 44
  • Slide 45
  • Slide 46
  • Slide 47
  • Slide 48
  • Slide 49
  • Graphs Simple graphs Algorithms Depth-first search Breadth-first search shortest path Connected components Directed graphs Weighted graphs Shortest path Minimum Spanning Tree
  • Slide 50
  • Slide 51
  • Slide 52
  • Dijkstras Algorithm Finds all shortest paths given a source Solves single-source, single- destination, single-pair shortest path problem Intuition: grows the paths from the source node using a greedy approach
  • Slide 53
  • Shortest Paths Dijkstras 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 54
  • Data structures 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 55
  • Dijkstras algorithm
  • Slide 56
  • Slide 57
  • Slide 58
  • Slide 59
  • Graphs Simple graphs Algorithms Depth-first search Breadth-first search shortest path Connected components Directed graphs Weighted graphs Shortest path Minimum Spanning Tree
  • Slide 60
  • Depth-First Search (DFS) Nonrecursive algorithm Push s onto a stack Repeat until the stack is empty: remove the top vertex v, if not visited, mark as visited add all vs unvisited neighbors to the stack
  • Slide 61
  • Slide 62
  • Shortest Paths Dijkstras 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 63
  • Bonus Question Adjacency list A -- B, D B -- C C -- E, G D -- E, F E -- B F -- G G -- E Show the sequence of vertices visited using BFS and DFS Show the shortest path length (and path) from A to each vertex
  • Slide 64
  • MapQuest 10 15 20 ? 15 5 18 25 33 Shortest path for a single source-target pair Dijkstra algorithm can be used
  • Slide 65
  • 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 66
  • 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 67
  • 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 15 5 18 25 33 n g(n) h(n)
  • Slide 68
  • 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 69
  • 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 70
  • Project Option: Emory MapQuest Implement Dijkstras algorithm for finding a route between two cities Bonus: A* algorithm
  • Slide 71
  • Graphs Simple graphs Algorithms Depth-first search Breadth-first search shortest path Connected components Directed graphs Weighted graphs Shortest path Minimum Spanning Tree
  • Slide 72
  • Slide 73
  • Slide 74
  • Slide 75
  • Slide 76
  • Applications Phone/cable network design minimum cost Approximation algorithms for NP-hard problems
  • Slide 77
  • Slide 78
  • Slide 79
  • Slide 80
  • Slide 81
  • Slide 82
  • Slide 83
  • Slide 84
  • Slide 85