Programming Abstractions

Slides:



Advertisements
Similar presentations
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
Advertisements

O(N 1.5 ) divide-and-conquer technique for Minimum Spanning Tree problem Step 1: Divide the graph into  N sub-graph by clustering. Step 2: Solve each.
Minimum Spanning Tree Sarah Brubaker Tuesday 4/22/8.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graph II MST, Shortest Path. Graph Terminology Node (vertex) Edge (arc) Directed graph, undirected graph Degree, in-degree, out-degree Subgraph Simple.
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.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
1 Minimum Spanning Trees Longin Jan Latecki Temple University based on slides by David Matuszek, UPenn, Rose Hoberman, CMU, Bing Liu, U. of Illinois, Boting.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
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:
ADA: 10. MSTs1 Objective o look at two algorithms for finding mimimum spanning trees (MSTs) over graphs Prim's algorithm, Kruskal's algorithm Algorithm.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Minimum Spanning Trees
COSC 2007 Data Structures II Chapter 14 Graphs III.
Minimum spanning trees Aims: To know the terms: tree, spanning tree, minimum spanning tree. To understand that a minimum spanning tree connects a network.
1 Minimum Spanning Trees (some material adapted from slides by Peter Lee, Ananda Guna, Bettina Speckmann)
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.
Minimum- Spanning Trees
Prims Algorithm for finding a minimum spanning tree
Spanning Trees Dijkstra (Unit 10) SOL: DM.2 Classwork worksheet Homework (day 70) Worksheet Quiz next block.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Graphs. Introduction Graphs are a collection of vertices and edges Graphs are a collection of vertices and edges The solid circles are the vertices A,
Graph Search Applications, Minimum Spanning Tree
Minimum Spanning Trees
Graphs Representation, BFS, DFS
COMP108 Algorithmic Foundations Greedy methods
Programming Abstractions
Programming Abstractions
12. Graphs and Trees 2 Summary
CS 106B Homework 7: Trailblazer
Spanning Trees Lecture 21 CS2110 – Fall 2016.
COMP 6/4030 ALGORITHMS Prim’s Theorem 10/26/2000.
Minimum Spanning Trees and Shortest Paths
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Three Graph Algorithms
Programming Abstractions
CS120 Graphs.
Short paths and spanning trees
Programming Abstractions
Graph Algorithm.
CSE 421: Introduction to Algorithms
Minimum Spanning Tree.
Minimum Spanning Trees
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.
Graphs.
Chapter 11 Graphs.
CSE332: Data Abstractions Lecture 18: Minimum Spanning Trees
Minimum Spanning Trees
Minimum Spanning Tree.
Algorithms Searching in a Graph.
CSE 373: Data Structures and Algorithms
Graphs.
Minimum spanning trees
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.
Prim’s algorithm for minimum spanning trees
CSE 373: Data Structures and Algorithms
Lecture 14 Minimum Spanning Tree (cont’d)
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 (continued) Spoiler: just a very, very small change to Dijkstra’s Minimum Spanning Tree Kruskal’s algorithm

A* Solving Super Mario (video) https://youtu.be/DlkMs4ZHHr8

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

A* Solving Super Mario (video) https://youtu.be/DlkMs4ZHHr8

Minimum Spanning Tree

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

Prim’s Algorithm

Prim’s algorithm Arbitrarily choose start vertex Add start vertex to MST While vertices in MST < total vertices: Examine all edges that leave the current MST Choose the smallest one Add the end vertex of that edge to the MST

Prim’s algorithm

Kruskal’s Algorithm

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

Efficiency of this step is key 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 Efficiency of this step is key

Cluster management questions The assignment handout asks you to consider questions such as: How will you keep track of which nodes are in each cluster? How will you determine which cluster a node belongs to? How will you merge together two clusters?

Cluster management strategies [watch lecture for whiteboard hints]

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