Programming Abstractions

Slides:



Advertisements
Similar presentations
Problem solving with graph search
Advertisements

CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
1 Discrete Structures & Algorithms Graphs and Trees: III EECE 320.
Chapter 8, Part I Graph Algorithms.
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Dijkstra’s Algorithm: single source shortest paths David Kauchak cs62 Spring 2010.
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. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Proof of correctness of Dijkstra’s algorithm: Basically, we need to prove two claims. (1)Let S be the set of vertices for which the shortest path from.
Programming Abstractions Cynthia Lee CS106B. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Graphs - II CS 2110, Spring Where did David leave that book? 2.
CSC 172 DATA STRUCTURES.
Graphs A New Data Structure
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
Programming Abstractions
Programming Abstractions
CSC317 Graph algorithms Why bother?
Cse 373 May 8th – Dijkstras.
CS 106B Homework 7: Trailblazer
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Ellen Walker CPSC 201 Data Structures Hiram College
Programming Abstractions
Short paths and spanning trees
Programming Abstractions
CSE 421: Introduction to Algorithms
Elementary Graph Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Dijkstra’s Algorithm We are given a directed weighted graph
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Spanning Trees.
Graph Representation (23.1/22.1)
Graphs.
Chapter 11 Graphs.
Yan Shi CS/SE 2630 Lecture Notes
CSE 421: Introduction to Algorithms
Richard Anderson Autumn 2016 Lecture 5
Slide Courtesy: Uwash, UT
CSE 373: Data Structures and Algorithms
Algorithms Searching in a Graph.
CSE332: Data Abstractions Lecture 17: Shortest Paths
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Graphs.
Graphs.
CSE 373: Data Structures and Algorithms
Graphs.
Richard Anderson Spring 2016
Graphs.
Algorithm Course Dr. Aref Rashad
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:

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

Breadth-First Search We’ve seen BFS before this quarter!

Word Ladder Assignment Θ BFS in this class so far Word Ladder Assignment A Maze B C D E F Trees Slime Mold

BFS Code (Maze version) 1. Make a Queue to remember places we want to explore in the future 2. Enqueue starting point 4. Dequeue a point and visit it While there are still things in the queue, keep exploring! 5. Enqueue any new points you discover while visiting the current point

Breadth-First Search in a Graph Graph algorithms

BFS is useful for finding the shortest path between two nodes. Breadth-First Search A B C D BFS is useful for finding the shortest path between two nodes. Example: What is the shortest way to go from F to G? E F G H I J K L

BFS is useful for finding the shortest path between two nodes. Breadth-First Search A B C D BFS is useful for finding the shortest path between two nodes. Example: What is the shortest way to go from F to G? Way 1: F->E->I->G 3 edges E F G H I J K L

BFS is useful for finding the shortest path between two nodes. Breadth-First Search A B C D BFS is useful for finding the shortest path between two nodes. Example: What is the shortest way to go from F to G? Way 2: F->K->G 2 edges E F G H I J K L

BFS is useful for finding the shortest path between two nodes. Map Example: What is the shortest way to go from Yoesmite (F) to Palo Alto (J)?

Breadth-First Search Yoesmite TO START: Color all nodes GREY Queue is empty Yoesmite E E F F G G H H Palo Alto I I J J K K L L

Breadth-First Search TO START (2): Enqueue the desired start node Note that anytime we enqueue a node, we mark it YELLOW E E F F G G H H I I J J K K L L F

Mark current node GREEN Breadth-First Search A A B B C C D D LOOP PROCEDURE: Dequeue a node Mark current node GREEN Set current node’s GREY neighbors’ parent pointers to current node, then enqueue them (remember: set them YELLOW) F E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D F E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D F E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D F E E F F G G H H I I J J K K L L

Breadth-First Search A A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search A A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search A A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search B A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search B A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search B A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search D A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search D A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search D A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D E E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D E E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D E E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D E E F F G G H H K I I J J K K L L

Breadth-First Search A A B B C C D D E E F F G G H H K I I J J K K L L

Breadth-First Search You predict the next slide! K’s neighbors F,G,H are yellow and in the queue and their parents are pointing to K K’s neighbors G,H are yellow and in the queue and their parents are pointing to K K’s neighbors G,H are yellow and in the queue Other/none/more A A B B C C D D E E F F G G H H K I I J J K K L L C H I

Breadth-First Search A A B B C C D D E E F F G G H H K I I J J K K L L

Breadth-First Search A A B B C C D D E E F F G G H H K I I J J K K L L

Breadth-First Search C A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search C A A B B C C D D E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D H E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D H E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D E E F F G G H H I I J J K K L L I

Breadth-First Search A A B B C C D D E E F F G G H H I I I J J K K L L

Breadth-First Search A A B B C C D D E E F F G G H H I I I J J K K L L

Breadth-First Search A A B B C C D D E E F F G G H H I I I J J K K L L

Breadth-First Search A A B B C C D D G E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D G E E F F G G H H I I J J K K L L

Breadth-First Search A A B B C C D D E E F F G G H H I I J J K K L L L

Breadth-First Search A A B B C C D D E E F F G G H H I I J J K K L L L

Breadth-First Search A A B B C C D D E E F F G G H H I I J J K K L L L

Breadth-First Search A A B B C C D D E E F F G G H H J I I J J K K L L

Breadth-First Search A A B B C C D D E E F F G G H H J I I J J K K L L

Breadth-First Search Done! Now we know that to go from Yoesmite (F) to Palo Alto (J), we should go: F->E->I->L->J (4 edges) (note we follow the parent pointers backwards) A A B B C C D D E E F F G G H H J I I J J K K L L

Breadth-First Search THINGS TO NOTICE: We used a queue What’s left is a kind of subset of the edges, in the form of ‘parent’ pointers If you follow the parent pointers from the desired end point, you will get back to the start point, and it will be the shortest way to do that E E F F G G H H I I J J K K L L

Quick question about efficiency… Let’s say that you have an extended family with somebody in every city in the western U.S.

Quick question about efficiency… You’re all going to fly to Yosemite for a family reunion, and then everyone will rent a car and drive home, and you’ve been tasked with making custom Yosemite-to-home driving directions for everyone.

Quick question about efficiency… You calculated the shortest path for yourself to return home from the reunion (Yosemite to Palo Alto) and let’s just say that it took time X = O((|E| + |V|)log|V|) With respect to the number of cities |V|, and the number of edges or road segments |E| How long will it take you, in total, to calculate the shortest path for you and all of your relatives? O(|V|*X) O(|E|*|V|* X) X Other/none/more

Breadth-First Search THINGS TO NOTICE: (4) We now have the answer to the question “What is the shortest path to you from F?” for every single node in the graph!! E E F F G G H H I I J J K K L L

Dijkstra’s Shortest Paths (Like Breadth-first Search, but takes into account weight/distance between nodes)

Edsger Dijkstra THE multiprogramming system (operating system) 1930-2002 THE multiprogramming system (operating system) Layers of abstraction!! Complier for a language that can do recursion Dining Philosopher’s Problem (resource contention and deadlock) Dijkstra’s algorithm “Goto considered harmful” (title given to his letter) This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license. http://en.wikipedia.org/wiki/File:Edsger_Wybe_Dijkstra.jpg

The Shortest Path Problem Suppose that you have a graph representing different locations Each edge has an associated cost (or weight, length, etc) We'll assume costs are nonnegative* Goal: Find the least-cost (or lowest-weight, lowest-length, etc) path from some node u to a node v * else use the Bellman–Ford algorithm

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

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