Semester 10 Time sure flies.. PotW Solution One possible solution is to randomly search the grid: o At each point in your search, look at the (up to four)

Slides:



Advertisements
Similar presentations
Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
Advertisements

Traffic assignment.
Problem solving with graph search
Traveling Salesperson Problem
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
ICS-171:Notes 4: 1 Notes 4: Optimal Search ICS 171 Summer 1999.
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
1 Chapter 15.3 Hamilton Paths and Hamilton Circuits Objectives 1.Understand the definitions of Hamilton paths & Hamilton circuits. 2.Find the number of.
More Graph Algorithms Minimum Spanning Trees, Shortest Path Algorithms.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
1 Greedy Algorithms. 2 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking algorithms Divide.
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Tracking Moving Objects in Anonymized Trajectories Nikolay Vyahhi 1, Spiridon Bakiras 2, Panos Kalnis 3, and Gabriel Ghinita 3 1 St. Petersburg State University.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Shortest Paths1 C B A E D F
Minimum Spanning Trees What is a MST (Minimum Spanning Tree) and how to find it with Prim’s algorithm and Kruskal’s algorithm.
Minimal Spanning Trees What is a minimal spanning tree (MST) and how to find one.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Greedy methods Prudence Wong
Graphs II Robin Burke GAM 376. Admin Skip the Lua topic.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Data Structures Week 9 Towards Weighted BFS So, far we have measured d s (v) in terms of number of edges in the path from s to v. Equivalent to assuming.
SPANNING TREES Lecture 21 CS2110 – Spring
Dr. Naveed Ahmad Assistant Professor Department of Computer Science University of Peshawar.
Shortest Path Problem Weight of the graph –Nonnegative real number assigned to the edges connecting to vertices Weighted graphs –When a graph.
Computer Science Club It is easier to change the specification to fit the program than vice versa.
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
Shortest Paths CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Po-Lung Chen (Dont block me) d091: Urban Transport System 2010/03/26 (1) d091: Urban Transport System Po-Lung Chen Team Dont Block Me, National Taiwan.
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
SPANNING TREES Lecture 21 CS2110 – Spring
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Path -Prim’s -Djikstra’s. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected.
School of Computing Clemson University Fall, 2012
Lecture 13 Shortest Path.
CSC317 Shortest path algorithms
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Programming Abstractions
Dijkstra’s Algorithm We are given a directed weighted graph
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
A* Path Finding Ref: A-star tutorial.
CSE 373: Data Structures and Algorithms
Outline This topic covers Prim’s algorithm:
Communication Networks
CSE 373: Data Structures and Algorithms
Shortest Paths.
CSE 373 Data Structures and Algorithms
Informed Search Idea: be smart about what paths to try.
CSE 417: Algorithms and Computational Complexity
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Lecture 12 Shortest Path.
Graphs: Shortest path and mst
CSE 332: Minimum Spanning Trees
Informed Search Idea: be smart about what paths to try.
Data Structures and Algorithm Analysis Lecture 8
Presentation transcript:

Semester 10 Time sure flies.

PotW Solution One possible solution is to randomly search the grid: o At each point in your search, look at the (up to four) empty neighboring spaces o For each neighbor, conduct a flood-fill, and count the number of empty spaces in that region This tells you the best possible result if you go in that direction You might call this the "heuristic" function o Out of the neighboring spaces with the maximal heuristic, pick a random one. o If you have no empty spaces, you restart the search, and keep looking until time runs out

PotW Solution - Example Orange is travelled path Red is suboptimal – green is better

General Shortest Path Info A shortest path between two nodes on a graph is exactly what it sounds like o "Distance" in this case is measured as the sum of edge weights Note that this measure is still well-defined if edges are: o unidirectional: you can only travel across certain edges in one direction o negative: travelling across the edge sends you backwards in time (!)

Dijkstra’s Shortest Path Algorithm! Given a source node s, Dijkstra's can quickly tell you the distance from a single source node to every other node Dijkstra's Algorithm works for all graphs, except those that have negative edges At each step of Dijkstra's, the algorithm maintains a list of distances, dist, from the source to every other node o dist is not quite finalized until the end of the algorithm o It is instead improved over time o dist(source,source) is initialized as 0 o dist(source, x) is initalized as infinity for all other x  not the shortest title

Dijkstra’s Algorithm (cont.) It is, in essence, a greedy algorithm o At each step of the algorithm, it finds the closest node, cur, and marks it as visited This node's distance is now finalized o It then updates all neighbors of cur, neigh Update dist(source,neigh) using cur as the intermediary node o This greediness only works if all edge weights are nonnegative "Updating" the distance from a to b given an intermediary m: o dist(a,b)=min(dist(a,b),dist(a,m)+dist(m,b)) o Note that order matters: dist(x,y) may not be the same as dist(y,x)

Performance Number of nodes n o Number of edges: m Note that you visit each node exactly once when you perform updates O(n 2 + m) with looping to find node with minimum distance o Look over each node O(n) times O((m + n) logn) with binary heaps - found in Java's PriorityQueue O(m + nlogn) with complex data structures o No advantage in practice over previous option o This is essentially optimal in terms of theoretical complexity

Using PriorityQueue O(n 2 +m) can be turned into O((m+n) logn): o The PriorityQueue data structure helps you maintain the smallest item in a dynamic (changing) set o In this case, it helps you find the closest node quickly o Every time you update a node's distance, add it to the queue: this takes O(mlogn) time Note that this allows duplicate items This is necessary because only the top item in the queue is accessible Just make sure you skip over duplicates when you query (peek) later on o Every time you query the closest node, pop it from the queue: this takes O(nlogn) time

Examples/Extensions To reconstruct the shortest path: o For each node, remember its "parent" node o Parent = the node that was last used to update its distance Usually, in contest problems, constructing the graph is the hardest part of the problem o What if you're allowed to magically skip (teleport across) up to k edges? o What if you multiply edge weights instead of adding? Extension: In 1994, David Eppstein published an elegant solution for the kth shortest path o Amazingly, its complexity is basically the same as Dijkstra's: O(m+nlogn+k)

USACO! Today is the last day to take the January USACO! o 4 hours instead of 3! (supposedly b/c problems are tougher than usual) o As usual, participation is worth 5 points of PotW credit December and January USACO problems will be covered next meeting

PotW: Round Trip Today, Bessie wants to take a trip to a city, but doesn't know where yet. She has x dollars to spend, and the airline she is using charges $1 per mile. Tell her how many cities she might be able to visit, if she starts at node #1. Please note that all possible flights are one- way only, and after visiting her destination, she must also return. She can also take as many flights as she wants. Please also include node #1 in your count.

Round Trip: Details Assume that every city is visitable from every other city given an infinite amount of money Also assume that all plane flight distances are less than 1000 Let N=# of cities, and M=# of flights For 25 points, solve for N<100 and M<100 For 40 points, solve for N<10000 and M<100000

Sample Input/Output Input: 11 (x) 4 6 (# of cities, # of flights) (flight #1: city #1, city #2, then distance in miles) (flight #2) (etc.) Output: 3 (she can visit nodes #1,2, or 3, but not 4)

Hints Note that this is very similar to a standard Dijkstra's problem o cities = nodes, and flights = unidirectional edges o However, how do you take into account Bessie having to come back from her destination? Run Dijkstra's twice: o Once with the given edges, and once with the given edges, but reversed o For every city, add these two distances up, and if they're less than or equal to x, you can indeed visit the city.