Programming Abstractions

Slides:



Advertisements
Similar presentations
Problem solving with graph search
Advertisements

1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
Discussion #36 Spanning Trees
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
COSC 2007 Data Structures II Chapter 14 Graphs III.
Spanning Trees. A spanning tree for a connected, undirected graph G is a graph S consisting of the nodes of G together with enough edges of G such that:
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee Dijkstra’s animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia.
Programming Abstractions Cynthia Lee CS106X. Graphs Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
1 A* search Outline In this topic, we will look at the A* search algorithm: –It solves the single-source shortest path problem –Restricted to physical.
Graph Search Applications, Minimum Spanning Tree
Greedy Algorithms.
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
COMP108 Algorithmic Foundations Greedy methods
May 12th – Minimum Spanning Trees
Programming Abstractions
Shortest Paths and Minimum Spanning Trees
Programming Abstractions
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Programming Abstractions
Spanning Trees.
CSC317 Graph algorithms Why bother?
Cse 373 May 8th – Dijkstras.
CS 106B Homework 7: Trailblazer
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Single-Source Shortest Paths
Minimum Spanning Trees and Shortest Paths
Introduction to Graphs
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Programming Abstractions
Finding Heuristics Using Abstraction
Short paths and spanning trees
Graph Search Applications
Graph Algorithm.
CSE 421: Introduction to Algorithms
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Breadth-First Search (BFS)
CSE373: Data Structures & Algorithms Lecture 12: Minimum Spanning Trees Catie Baker Spring 2015.
CSE373: Data Structures & Algorithms Lecture 20: Minimum Spanning Trees Linda Shapiro Spring 2016.
Minimum Spanning Trees
CSE 373 Data Structures and Algorithms
Spanning Trees.
CSE 373: Data Structures and Algorithms
Graphs.
Advanced Analysis of Algorithms
Chapter 11 Graphs.
Shortest Paths and Minimum Spanning Trees
CSE 421: Introduction to Algorithms
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Artificial Intelligence
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Weighted Graphs & Shortest Paths
CSE332: Data Abstractions Lecture 17: Shortest Paths
Graphs.
Informed Search Idea: be smart about what paths to try.
CSE 417: Algorithms and Computational Complexity
CSE 373: Data Structures and Algorithms
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Algorithm Course Dr. Aref Rashad
Informed Search Idea: be smart about what paths to try.
Topological Sorting Minimum Spanning Trees Shortest Path
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

Graphs Topics Graphs! Basics What are they? How do we represent them? Theorems What are some things we can prove about graphs? Breadth-first search on a graph Spoiler: just a very, very small change to tree version Dijkstra’s shortest paths algorithm Spoiler: just a very, very small change to BFS A* shortest paths algorithm Spoiler: just a very, very small change to Dijkstra’s Minimum Spanning Tree Kruskal’s algorithm

A A 0? 6 B B 3 C C 3 1 2 1 D D 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

A A 0? 6 B B 3 C C A 0? 3 1 2 1 D D 1 E E 7 F F 9 4 4 7 G G 2 H H 5 I I

A A 0? 6 B B 3 C C A 0? 3 1 2 1 D D 1 E E 7 F F 9 4 4 7 G G 2 H H 5 I I

A A 0? 6 B B 3 C C 3 1 2 1 D D 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

A A 6 B B 3 C C 3 1 2 1 D D 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

A A 6 B B 6? 3 C C 3 1 2 1 D D 3? 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

A A B B 6? C C D 3? B 6? D D 3? E E F F G G H H I I 6 B B 6? 3 C C D 3? 3 1 1 2 B 6? D D 3? 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

A A B B 6? C C D 3? B 6? D D 3? E E F F G G H H I I 6 B B 6? 3 C C D 3? 3 1 1 2 B 6? D D 3? 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

A A B B 6? C C D 3? B 6? D D 3? E E F F G G H H I I 6 B B 6? 3 C C D 3? 3 1 1 2 B 6? D D 3? 1 E E 7 F F 9 4 7 4 G G 2 H H 5 I I

A A 6 B B 6? 3 C C B 6? 3 1 1 2 D D 3? 1 E E 7 F F 9 4 4 7 G G 2 H H 5 I I

A A 6 B B 6? 3 C C B 6? 3 1 1 2 D D 3 1 E E 7 F F 9 4 4 7 G G 2 H H 5 I I

A A 6 B 6? B 3 C C B 6? 3 1 1 2 D D 3 1 E 4? E 7 F F 9 4 7 4 G G 12? 2 H H 5 I I

A A 6 B 6? B 3 C C 3 1 1 2 B 6? D D 3 1 E 4? E 7 F F 9 4 7 4 G G 12? 2 H H 5 I I

A A B B 6? C C E 4? B 6? D D 3 E 4? E F F G 12? G G 12? H H I I 6 B B 6? 3 C C E 4? 3 1 1 2 B 6? D D 3 1 E 4? E 7 F F G 12? 9 4 4 7 G G 12? 2 H H 5 I I

A A B B 6? C C E 4? B 6? D D 3 E 4? E F F G 12? G G 12? H H I I 6 B B 6? 3 C C E 4? 3 1 1 2 B 6? D D 3 1 E 4? E 7 F F G 12? 9 4 4 7 G G 12? 2 H H 5 I I

A A B B 6? C C E 4? B 6? D D 3 E 4? E F F G 12? G G 12? H H I I 6 B B 6? 3 C C E 4? 3 1 1 2 B 6? D D 3 1 E 4? E 7 F F G 12? 9 4 4 7 G G 12? 2 H H 5 I I

A A B B 6? C C B 6? G 12? D D 3 E 4? E F F G G 12? H H I I 6 B B 6? 3 C C B 6? 3 1 1 2 G 12? D D 3 1 E 4? E 7 F F 9 4 7 4 G G 12? 2 H H 5 I I

A A B B 6? C C B 6? G 12? D D 3 E 4 E F F G G 12? H H I I 6 B B 6? 3 C C B 6? 3 1 1 2 G 12? D D 3 1 E 4 E 7 F F 9 4 7 4 G G 12? 2 H H 5 I I

A A B 6? B C C B 6? G 12? D D 3 E E 4 F F 11? G G 12? H 8? H I I 6 B 6? B 3 C C B 6? 3 1 1 2 G 12? D D 3 1 E E 4 7 F F 11? 9 4 7 4 G G 12? 2 H 8? H 5 I I

A A B 6? B C C B 6? D D 3 E E 4 F F 11? G 12? G G 12? H H 8? I I 6 B 6? B 3 C C B 6? 3 1 1 2 D D 3 1 E E 4 7 F F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

A A B 6? B C C B 6? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H 6 B 6? B 3 C C B 6? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

A A B 6? B C C B 6? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H 6 B 6? B 3 C C B 6? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

A A B 5? B C C B 6? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H 6 B 5? B 3 C C B 6? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

A A B 5? B C C B 5? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H 6 B 5? B 3 C C B 5? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

A A B 5? B C C B 5? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H 6 B 5? B 3 C C B 5? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

A A B 5? B C C B 5? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H 6 B 5? B 3 C C B 5? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

A A B 5? B C C B 5? H 8? D D 3 E E 4 F F 11? F 11? G 12? G G 12? H H 6 B 5? B 3 C C B 5? 3 1 1 2 H 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

A A B 5? B C C H 8? F 11? D D 3 E 4 E F F 11? G 12? G G 12? H H 8? I I 6 B 5? B 3 C C H 8? 3 1 1 2 F 11? D D 3 1 E 4 E 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H H 8? 5 I I

A A B 5 B C C H 8? F 11? D D 3 E 4 E F F 11? G 12? G G 12? H H 8? I I 6 B 5 B 3 C C H 8? 3 1 1 2 F 11? D D 3 1 E 4 E 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H H 8? 5 I I

A A B 5 B C 8? C H 8? F 11? D D 3 E E 4 F F 11? G 12? G G 12? H H 8? I 6 B 5 B 3 C 8? C H 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H H 8? 5 I I

A A B 5 B C 8? C H 8? D D 3 E E 4 F 11? F F 11? G 12? G G 12? H H 8? I 6 B 5 B 3 C 8? C H 8? 3 1 2 1 D D 3 1 E E 4 7 F 11? F F 11? G 12? 9 4 7 4 G G 12? 2 H H 8? 5 I I

A A 6 B 5 B 3 C 8? C H 8? 3 1 2 1 C 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H 8? H 5 I I

A A 6 B 5 B 3 C 8? C H 8? 3 1 2 1 C 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H 8? H 5 I I

A A 6 B 5 B 3 C 8? C H 8? 3 1 2 1 C 8? D D 3 1 E E 4 7 F F 11? F 11? G 12? 9 4 7 4 G G 12? 2 H 8? H 5 I I

You predict the next queue state: H,C,F,G,I C,F,G,I C,G,F,I Other/none/ more A A 6 B 5 B 3 C C 8? H 8? 3 1 1 2 C 8? D D 3 1 E E 4 7 F 11? F F 11? G 12? 9 4 7 4 G G 12? 2 H 8? H 5 I I

A A B 5 B C 8? C C 8? F 11? D D 3 E E 4 F F 11? G 12? G G 12? H 8? H I 6 B 5 B 3 C 8? C C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H 8? H 5 I I

A A B 5 B C 8? C C 8? F 11? D D 3 E E 4 F F 11? G 12? G G 12? H 8 H I 6 B 5 B 3 C 8? C C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F F 11? G 12? 9 4 4 7 G G 12? 2 H 8 H 5 I I

A A B B 5 C 8? C C 8? F 11? D D 3 E E 4 F 11? F G 12? G G 12? H H 8? H 6 B B 5 3 C 8? C C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 12? 9 4 4 7 G G 12? 2 H H 8? H 8 5 I 13? I

A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 12? I 13? 9 4 4 7 G G 12? 2 H H 8? H 8 5 I I 13?

A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 12? I 13? 9 4 4 7 G G 12? 2 H H 8? H 8 5 I I 13?

A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 12? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 10? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 F 11? D D 3 1 E E 4 7 F 11? F G 10? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

A A 6 B B 5 3 C 8? C C 8? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? I 13? 9 4 7 4 G G 10? 2 H 8 H 8? H 5 I I 13?

A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

A A 6 B 5 B 3 C C 8? C 8? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? 9 4 4 7 G G 10? 2 H H 8? H 8 5 I I 13?

A A 6 B B 5 3 C 8? C C 8? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? 9 4 7 4 G G 10? 2 H H 8 5 I 13? I

A A 6 B B 5 3 C C 8? C 8? 3 1 1 G 10? D D 3 1 E E 4 7 F 11? F F 11? I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C C 8? G 10? F 11? D D 3 E E 4 F 11? F I 13? G G 10? H H 8 I 6 B B 5 3 C C 8? G 10? 3 1 1 F 11? D D 3 1 E E 4 7 F 11? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C C 8 G 10? F 11? D D 3 E E 4 F 11? F I 13? G G 10? H H 8 I 6 B B 5 3 C C 8 G 10? 3 1 1 F 11? D D 3 1 E E 4 7 F 11? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C C 8 G 10? F 11? D D 3 E E 4 F 11? F I 13? G G 10? H H 8 I 6 B B 5 3 C C 8 G 10? 3 1 1 F 11? D D 3 1 E E 4 7 F 11? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C C 8 G 10? F 9? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I 6 B B 5 3 C C 8 G 10? 3 1 1 F 9? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C C 8 G 10? F 9? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I 6 B B 5 3 C C 8 G 10? 3 1 1 F 9? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C C 8 F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I 6 B B 5 3 C C 8 F 9? 3 1 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C C 8 F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I 6 B B 5 3 C C 8 F 9? 3 1 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C C 8 F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I 6 B B 5 3 C C 8 F 9? 3 1 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 4 7 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C 8 C F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I 6 B B 5 3 C 8 C F 9? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 7 4 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C 8 C F 9? G 10? D D 3 E E 4 F 9? F I 13? G G 10? H H 8 I 6 B B 5 3 C 8 C F 9? 3 1 2 1 G 10? D D 3 1 E E 4 7 F 9? F I 13? 9 4 7 4 G G 10? 2 H H 8 5 I 13? I

A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9? F G G 10? H H 8 I I 13? 6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9? F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13? 6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13? 6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13? 6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13? 6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13? 6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

A A B B 5 C 8 C G 10? I 13? D D 3 E 4 E F 9 F G G 10? H H 8 I I 13? 6 B B 5 3 C 8 C G 10? 3 1 1 2 I 13? D D 3 1 E 4 E 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I I 13?

A A 6 B B 5 3 C C 8 I 13? 3 1 1 2 D D 3 1 E E 4 7 F 9 F 9 4 7 4 G G 10? 2 H H 8 5 I 13? I

A A 6 B B 5 3 C C 8 I 13? 3 1 1 2 D D 3 1 E E 4 7 F 9 F 9 4 7 4 G G 10 2 H H 8 5 I 13? I

A A 6 B B 5 3 C C 8 I 13? 3 1 1 2 D D 3 1 E E 4 7 F 9 F 9 4 7 4 G G 10 2 H H 8 5 I 13? I

A A 6 B B 5 3 C C 8 I 13? 3 1 1 2 D D 3 1 E E 4 7 F 9 F 9 4 7 4 G G 10 2 H H 8 5 I 13? I

A A 6 B 5 B 3 C 8 C 3 1 2 1 D D 3 1 E E 4 7 F 9 F 9 4 4 7 G G 10 2 H 8 H 5 I 13? I

A A 6 B 5 B 3 C 8 C 3 1 2 1 D D 3 1 E E 4 7 F 9 F 9 4 4 7 G G 10 2 H 8 H 5 I 13 I

A A 6 B 5 B 3 C 8 C 3 1 2 1 D D 3 1 E E 4 7 F 9 F 9 4 4 7 G G 10 2 H 8 H 5 I 13 I

Dijkstra's Algorithm Split nodes apart into three groups: Green nodes, where we already have the shortest path; Gray nodes, which we have never seen; and Yellow nodes that we still need to process. Dijkstra's algorithm works as follows: Mark all nodes gray except the start node, which is yellow and has cost 0. Until no yellow nodes remain: Choose the yellow node with the lowest total cost. Mark that node green. Mark all its gray neighbors yellow and with the appropriate cost. Update the costs of all adjacent yellow nodes by considering the path through the current node.

An Important Note The version of Dijkstra's algorithm I have just described is not the same as the version described in the course reader. This version is more complex than the book's version, but is much faster. THIS IS THE VERSION YOU MUST USE ON YOUR TRAILBLAZER ASSIGNMENT!

How Dijkstra's Works Dijkstra's algorithm works by incrementally computing the shortest path to intermediary nodes in the graph in case they prove to be useful. Most of these nodes are completely in the wrong direction. No “big-picture” conception of how to get to the destination – the algorithm explores outward in all directions. Could we give the algorithm a hint?

Dijkstra’s: SPIN analysis (shoutout to GSB students) Situation: Dijkstra's algorithm works by incrementally computing the shortest path to intermediary nodes in the graph in case they prove to be useful. Problem: No big-picture conception of how to get to the destination – the algorithm explores outward in all directions, “in case.” Implication: Most of these explored nodes will end up being in completely the wrong direction. Need: Could we give the algorithm a “hint” of which direction to go?

A* and Dijkstra’s Close cousins

predicted-distance ≤ actual-distance Heuristics In the context of graph searches, a heuristic function is a function that guesses the distance from some known node to the destination node. The guess doesn't have to be correct, but it should try to be as accurate as possible. Examples: For Google Maps, a heuristic for estimating distance might be the straight-line “as the crow flies” distance. Admissible Heuristics A heuristic function is called an admissible heuristic if it never overestimates the distance from any node to the destination. In other words: predicted-distance ≤ actual-distance

priority(u) = distance(s, u) + heuristic(u, t) Why Heuristics Matter We can modify Dijkstra's algorithm by introducing heuristic functions. Given any node u, there are two associated costs: The actual distance from the start node s. The heuristic distance from u to the end node t. Key idea: Run Dijkstra's algorithm, but use the following priority in the priority queue: priority(u) = distance(s, u) + heuristic(u, t) This modification of Dijkstra's algorithm is called the A* search algorithm. s u t

Focuses work in areas likely to be productive. A* Search As long as the heuristic is admissible (and satisfies one other technical condition), A* will always find the shortest path from the source to the destination node. Can be dramatically faster than Dijkstra's algorithm. Focuses work in areas likely to be productive. Avoids solutions that appear worse until there is evidence they may be appropriate.

Mark all nodes as gray. Mark the initial node s as yellow and at candidate distance 0. Enqueue s into the priority queue with priority 0. While not all nodes have been visited: Dequeue the lowest-cost node u from the priority queue. Color u green. The candidate distance d that is currently stored for node u is the length of the shortest path from s to u. If u is the destination node t, you have found the shortest path from s to t and are done. For each node v connected to u by an edge of length L: If v is gray: Color v yellow. Mark v's distance as d + L. Set v's parent to be u. Enqueue v into the priority queue with priority d + L. If v is yellow and the candidate distance to v is greater than d + L: Update v's candidate distance to be d + L. Update v's parent to be u. Update v's priority in the priority queue to d + L. Dijkstra's Algorithm

Mark all nodes as gray. Mark the initial node s as yellow and at candidate distance 0. Enqueue s into the priority queue with priority h(s,t). While not all nodes have been visited: Dequeue the lowest-cost node u from the priority queue. Color u green. The candidate distance d that is currently stored for node u is the length of the shortest path from s to u. If u is the destination node t, you have found the shortest path from s to t and are done. For each node v connected to u by an edge of length L: If v is gray: Color v yellow. Mark v's distance as d + L. Set v's parent to be u. Enqueue v into the priority queue with priority d + L + h(v,t). If v is yellow and the candidate distance to v is greater than d + L: Update v's candidate distance to be d + L. Update v's parent to be u. Update v's priority in the priority queue to d + L + h(v,t). A* Search

A* on two points where the heuristic is slightly misleading due to a wall blocking the way

A* starts with start node yellow, other nodes grey.

A*: dequeue start node, turns green.

A*: enqueue neighbors with candidate distance + heuristic distance as the priority value. 1 + 6? 1 + 6? 1 + 4? 1 + 6?

A*: dequeue min-priority-value node. 1 + 6? 1 + 6? 1 1 + 6?

What goes in the ? 2 + 5? 1 + 6? 2 + 4? Other/none/more ??? 1 + 6? 2 +

A*: enqueue neighbors. 1 + 6? 2 + 5? 1 + 6? 1 2 + 3? 1 + 6? 2 + 5?

1 + 6? 2 + 5? Now we’re done with the green “1” node’s turn. What is the next node to turn green? (and what would it be if this were Dijkstra’s?) 1 + 6? 1 2 + 3? 1 + 6? 2 + 5?

A. : dequeue next lowest priority value node A*: dequeue next lowest priority value node. Notice we are making a straight line right for the end point, not wasting time with other directions. 1 + 6? 2 + 5? 1 + 6? 1 2 1 + 6? 2 + 5?

A*: enqueue neighbors—uh-oh, wall blocks us from continuing forward. 1 + 6? 2 + 5? 3 + 4? 1 + 6? 1 2 1 + 6? 2 + 5? 3 + 4?

A*: eventually figures out how to go around the wall, with some waste in each direction. 3 + 8? 4 + 7? 5 + 6? 6 + 5? 7 + 4? 3 + 8? 2 3 4 5 6 7 + 2? 3 + 8? 2 1 2 3 7 + 2? 3 + 8? 2 1 1 2 8 3 + 8? 2 1 2 3 7 8 + 1? 3 + 8? 2 3 4 5 6 7 8 + 3? 3 + 8? 4 + 7? 5 + 6? 6 + 5? 7 + 4? 8 + 3?

For Comparison: What Dijkstra's Algorithm Would Have Searched 8 7 6 5 4 5 6 7 8 9? 7 6 5 4 3 4 5 6 7 8 9? 6 5 4 3 2 3 4 5 6 7 8 9? 5 4 3 2 1 2 3 7 8 9? 4 3 2 1 1 2 8 5 4 3 2 1 2 3 7 8 9? 6 5 4 4 3 2 3 4 5 6 7 8 9? 7 6 5 4 3 4 5 6 7 8 9? 8 7 6 5 4 5 6 7 8 9?

Mark all nodes as gray. Mark the initial node s as yellow and at candidate distance 0. Enqueue s into the priority queue with priority 0. While not all nodes have been visited: Dequeue the lowest-cost node u from the priority queue. Color u green. The candidate distance d that is currently stored for node u is the length of the shortest path from s to u. If u is the destination node t, you have found the shortest path from s to t and are done. For each node v connected to u by an edge of length L: If v is gray: Color v yellow. Mark v's distance as d + L. Set v's parent to be u. Enqueue v into the priority queue with priority d + L. If v is yellow and the candidate distance to v is greater than d + L: Update v's candidate distance to be d + L. Update v's parent to be u. Update v's priority in the priority queue to d + L. Dijkstra's Algorithm

Mark all nodes as gray. Mark the initial node s as yellow and at candidate distance 0. Enqueue s into the priority queue with priority h(s,t). While not all nodes have been visited: Dequeue the lowest-cost node u from the priority queue. Color u green. The candidate distance d that is currently stored for node u is the length of the shortest path from s to u. If u is the destination node t, you have found the shortest path from s to t and are done. For each node v connected to u by an edge of length L: If v is gray: Color v yellow. Mark v's distance as d + L. Set v's parent to be u. Enqueue v into the priority queue with priority d + L + h(v,t). If v is yellow and the candidate distance to v is greater than d + L: Update v's candidate distance to be d + L. Update v's parent to be u. Update v's priority in the priority queue to d + L + h(v,t). A* Search

Minimum Spanning Tree

A spanning tree in an undirected graph is a set of edges with no cycles that connects all nodes. A minimum spanning tree (or MST) is a spanning tree with the least total cost.

How many distinct minimum spanning trees are in this graph? B D F 1 7 3 3 Edges: (A,B)=1 (A,C)=3 (B,C)=6 (B,D)=3 (C,E)=3 (D,E)=3 (D,F)=7 A C E 3 3 How many distinct minimum spanning trees are in this graph? 0-1 6-7 2-3 >7 4-5

Kruskal’s algorithm Remove all edges from graph Place all edges in a PQ based on length/weight While !PQ.isEmpty(): Dequeue edge If the edge connects previous disconnected nodes or groups of nodes, keep the edge Otherwise discard the edge

Kruskal’s algorithm

The Good Will Hunting Problem

Video Clip https://www.youtube.com/watch?v=N7b0cLn-wHU

“Draw all the homeomorphically irreducible trees with n=10.”

“Draw all the homeomorphically irreducible trees with n=10.” In this case “trees” simply means graphs with no cycles “with n = 10” (i.e., has 10 nodes) “homeomorphically irreducible” No nodes of degree 2 allowed in your solutions For this problem, nodes of degree 2 are useless in terms of tree structure—they just act as a blip on an edge—and are therefore banned Have to be actually different Ignore superficial changes in rotation or angles of drawing