CS 106B Homework 7: Trailblazer

Slides:



Advertisements
Similar presentations
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
GRAPHS Lecture 18 CS2110 – Fall Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs - II CS 2110, Spring Where did I leave that book?
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.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Alyce Brady CS 510: Computer Algorithms Breadth-First Graph Traversal Algorithm.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Course notes CS2606: Data Structures and Object-Oriented Development Graphs Department of Computer Science Virginia Tech Spring 2008 (The following notes.
UNCA CSCI November, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
WEIGHTED GRAPHS. Weighted Graphs zGraph G = (V,E) such that there are weights/costs associated with each edge Õw((a,b)): cost of edge (a,b) Õrepresentation:
The scenario: have an army of ants (or whatever) all starting at the START vertex (say, A), moving at unit speed away from it in all directions. Whenever.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
SPANNING TREES Lecture 21 CS2110 – Spring
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Minimum- Spanning Trees
Graphs - II CS 2110, Spring Where did David leave that book? 2.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Graph Search Applications, Minimum Spanning Tree
CSE 373 Data Structures and Algorithms
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
Data Structures and Algorithms I
Fundamentals, Terminology, Traversal, Algorithms
Programming Abstractions
Programming Abstractions
Programming Abstractions
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Programming Abstractions
Short paths and spanning trees
Programming Abstractions
Graph Search Lecture 17 CS 2110 Spring 2018.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
CSE 373 Data Structures and Algorithms
Spanning Trees.
Can you get there from here?
CSE 373: Data Structures and Algorithms
Graphs.
Chapter 11 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Slide Courtesy: Uwash, UT
COMP171 Depth-First Search.
CSE 373: Data Structures and Algorithms
Breadth First Search - A B C D E F G H I front FIFO Queue.
CSE 373: Data Structures and Algorithms
Shortest Path Algorithm for Weighted Non-negative Undirected Graphs
Weighted Graphs & Shortest Paths
Slide Courtesy: Uwash, UT
Chapter 16 1 – Graphs Graph Categories Strong Components
Dijkstra's Shortest Path Algorithm
Minimum Spanning Trees (MSTs)
CSE 373: Data Structures and Algorithms
Spanning Trees Lecture 20 CS2110 – Spring 2015.
CSE 373: Data Structures and Algorithms
Graphs: Shortest path and mst
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 .
Presentation transcript:

CS 106B Homework 7: Trailblazer This document is copyright (C) Marty Stepp and Stanford Computer Science. Licensed under Creative Commons Attribution 2.5 License. All rights reserved.

Screenshots

Functions to write // four path-searching algorithms Vector<Vertex*> depthFirstSearch(BasicGraph& graph, Vertex* start, Vertex* end) breadthFirstSearch(...) dijkstrasAlgorithm(...) aStar(...) // method for making random mazes by producing // a minimum spanning tree of the graph Set<Edge*> kruskal(BasicGraph& graph)

DFS function dfs(v1, v2): reset graph data. dfsHelper(v1, v2, {}). function dfsHelper(v1, v2, path): add v1 to path. mark v1 as visited. if v1 is v2: a path is found! for each unvisited neighbor n of v1: if dfsHelper(n, v2, path) finds a path: hooray, a path is found! // if we get here, no path was found here. remove v1 from path.

BFS function bfs(v1, v2): queue := {v1}. while queue is not empty: dequeue a vertex v from the queue. mark v as visited. if v is v2: a path is found! reconstructPath(v1, v2). else for each unvisited neighbor n of v: set n's previous to be v. enqueue n in queue. function reconstructPath(v1, v2): v := v2. while v does not equal v1: insert v at front of path. v = v's previous.

Dijkstra's Algorithm function dijkstra(v1, v2): initialize every vertex to have a cost of infinity. set v1's cost to 0. pqueue := {v1, at priority 0}. while pqueue is not empty: v := dequeue vertex from pqueue with minimum priority. mark v as visited. if v is v2: reconstructPath(v1, v2). else for each unvisited neighbor n of v: cost := v's cost + weight of edge (v, n). if cost < n's cost: set n's cost to cost, and n's previous to v. enqueue n in the pqueue with priority of cost, or update priority to cost if it was already in the pqueue.

A* Search function aStar(v1, v2): initialize every vertex to have a cost of infinity. set v1's cost to 0. pqueue := {v1, at priority H(v1, v2)}. while pqueue is not empty: v := dequeue vertex from pqueue with minimum priority. mark v as visited. if v is v2: reconstructPath(v1, v2). else for each unvisited neighbor n of v: cost := v's cost + weight of edge (v, n). if cost < n's cost: set n's cost to cost, and n's previous to v. enqueue n in the pqueue with priority of (cost + H(v1, v2)), or update priority to (cost + H(v1, v2)) if it was already in the pqueue.

Kruskal's algorithm function kruskal(graph): // Place each vertex into its own "cluster" (group of reachable vertices). // Implement as a map from vertex* to sets of other vertex*.) clusters := {}. for each Vertex v in graph: clusters[v] = {v}. // Put all edges into a priority queue, using weights as priorities. pq := {}. for each Edge e in graph.getEdgeSet(): pq.enqueue(e, e->cost). MST := {}. while pq.size() > 1: Edge e := pq.dequeue(). if clusters[e->start] is different from clusters[e->finish]: // Merge the clusters containing the start and finish vertices of e. for each Vertex in clusters[e- >start]: add all of e->finish to that vertex's cluster. for each Vertex in clusters[e- >finish]: add all of e->start to that vertex's cluster. // Add this edge to the minimum spanning tree. MST += e. return MST.

Common bugs int vs double issues (round-off) always use double when computing costs / priorities algorithms only work once and fail the second time forgetting to call graph.resetData() at start of algorithm A* produces wrong answer; total length or cost is too high wrong priority/cost being used; cost != priority!! DFS crash on large graphs it's a stack overflow (this is okay) Kruskal is very slow maybe used the wrong data structure (see our pseudo solution)