CompSci 100e Program Design and Analysis II April 19, 2011 Prof. Rodger CompSci 100e, Spring 20111.

Slides:



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

§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
CS171 Introduction to Computer Science II Graphs Strike Back.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CPS 100, Fall Algorithms and Idioms l How do you avoid calculating the same thing over and over and over and over and …  Does it matter?  Recalculate.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
CPS Graphs, the Internet, and Everything
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.
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.
SPANNING TREES Lecture 21 CS2110 – Spring
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 5 Graph Algorithms.
Graphs Data Structures and Algorithms A. G. Malamos Reference Algorithms, 2006, S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani Introduction to Algorithms,Third.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Compsci 100, Spring Graphs, the Internet, and Everything
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.
Compsci 100, Spring Graphs, the Internet, and Everything
CPS 100, Spring Graphs, the Internet, and Everything
CompSci Problem: finding subsets l See CodeBloat APT, requires finding sums of all subsets  Given {72, 33, 41, 57, 25} what is sum closest (not.
Graphs A ‘Graph’ is a diagram that shows how things are connected together. It makes no attempt to draw actual paths or routes and scale is generally inconsequential.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
CompSci Graphs, the Internet, and Everything
CompSci The Internet l How valuable is a network? ä Metcalfe’s Law l Domain Name System: translates betweens names and IP addresses l Properties.
Compsci 101.2, Fall Plan for eleven-four l Thinking about APTs and test problems  How do you choose: list, string, set, dictionary  Experience?
CPS Graphs: Structures and Algorithms l How do packets of bits/information get routed on the internet  Message divided into packets on client.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
CompSci 100 Prog Design and Analysis II October 19, 2010 Prof. Rodger CompSci 100, Fall
Graphs A New Data Structure
Word Ladder APT From->[words]->to
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
Graphs, the Internet, and Everything
Graphs, the Internet, and Everything
Owen Astrachan Jeff Forbes December 6, 2018
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Graphs Representation, BFS, DFS
Graphs & Graph Algorithms 2
Chapter 11 Graphs.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CompSci 100e Program Design and Analysis II April 19, 2011 Prof. Rodger CompSci 100e, Spring 20111

Announcements Huffman due Thursday, April 21 APTS (3) due Tuesday, April 26 – Talk about AllWordLadders APT today, Internet APT next time – Can do extra APTs Extra credit assignments due Wed. April 27 NOTE: NO LATE ASSIGNMENTS accepted after Wed, April 27 11:59pm!!! CompSci 100e, Spring 20112

Word Ladder APT From->[words]->to – From hit to cog via [hot,dot,lot,dog,log] What words reachable from 'from'? – Repeat until we get to 'cog' Problem: reachable from 'dot' – Why not include 'hot'? – Don't re-use words Algorithm: – Find all words 1-away – From each n-away find (n+1)-away 3CompSci 100e, Spring 2011

Digression: word ladders How many ladders from cart to dire as shown? – Enqueue dare more than once? – Downside? Alternative? We want to know number of ladders that end at W. – What do we know initially? – When we put something on the queue, what do we know? – How do we keep track? Initialize and update per-word statistics cart care tart dart dare dirt dire

Word Ladder: more details # ladders that end at dare – At each word W Ladder length to W – Calculable from?? Maps? Queue? Sets? cart care tart dart dare dirt dire hire wire mire here were mere pere

Graphs, the Internet, and Everything 6CompSci 100e, Spring 2011

Graphs: Structures and Algorithms Mapquest, Tomtom, Garmin, Googlemap – How do you get from here to there? – What's a route? How is this known? What about The Oracle of Bacon, Erdos Numbers, and Word Ladders?The Oracle of BaconErdos Numbers – All can be modeled using graphs – What kind of connectivity does each concept model? Graphs are everywhere in the world (of algorithms?) – What is a graph? Algorithms on graphs? – Graph representation? 7CompSci 100e, Spring 2011

Sir Tim Berners-Lee I want you to realize that, if you can imagine a computer doing something, you can program a computer to do that. Unbounded opportunity... limited only by your imagination. And a couple of laws of physics. TCP/IP, HTTP – How, Why, What, When?

Vocabulary Graphs are collections of vertices and edges (vertex also called node) – Edge connects two vertices Direction can be important, directed edge, directed graph Edge may have associated weight/cost A vertex sequence v 0, v 1, …, v n-1 is a path where v k and v k+1 are connected by an edge. – If some vertex is repeated, the path is a cycle – A graph is connected if there is a path between any pair of vertices NYC Phil Boston Wash DC LGALAX ORD DCA $186 $412 $1701 $441

Graph questions/algorithms What vertices are reachable from a given vertex? – Two standard traversals: depth-first, breadth-first – connected components, groups of connected vertices Shortest path between two vertices (weighted graphs?) – BFS works, possibly uses more storage than DFS – Dijkstra’s algorithm efficient, uses a priority queue! Longest path in a graph – No known efficient algorithm Visit all vertices without repeating? Visit all edges? – With minimal cost? Hard! 10CompSci 100e, Spring 2011

Depth, Breadth, other traversals We want to visit every vertex that can be reached from a specific starting vertex (we might try all starting vertices) – Make sure we don't visit a vertex more than once Why isn't this an issue in trees? Mark vertex as visited, use set/array/map for this – Order in which vertices visited can be important – Storage/runtime efficiency of traversals important What other data structures do we have: stack, queue, … – What if we traverse using priority queue? 11CompSci 100e, Spring 2011

Breadth first search In an unweighted graph this finds the shortest path between a start vertex and every vertex – Visit every node one away from start – Visit every node two away from start This is nodes one away from a node one away – Visit every node three away from start, … Put vertex on queue to start (initially just one) – Repeat: dequeue vertex, enqueue adjacent vertices – Avoid enqueueing already visited/queued nodes – When are 1-away vertices enqueued? 2-away? N? – How many vertices on queue? 12CompSci 100e, Spring 2011

Code for breadth first public void breadth(String vertex){ Set visited = new TreeSet (); Queue q = new LinkedList (); q.add(vertex); visited.add(vertex); while (q.size() > 0) { String current = q.remove(); // process current for(each v adjacent to current){ if (!visited.contains(v)){// not visited visited.add(v); q.add(v); } CompSci 100e, Spring 2011

Pseudo-code for depth-first search void depthfirst(String vertex){ if (! alreadySeen(vertex)){ markAsSeen(vertex); System.out.println(vertex); for(each v adjacent to vertex) { depthfirst(v); } Clones are stacked up, problem? Can we make use of stack explicit? CompSci 100e, Spring 2011

BFS compared to DFS public Set bfs(String start){ Set visited = new TreeSetString>(); Queue qu = new LinkedList (); visited.add(start); qu.add(start); while (qu.size() > 0){ String v = qu.remove(); for(String adj : myGraph.getAdjacent(v)){ if (! visited.contains(adj)) { visited.add(adj); qu.add(adj); } return visited; } 15CompSci 100e, Spring 2011

BFS becomes DFS public Set dfs(String start){ Set visited = new TreeSet (); Queue qu = new LinkedList (); visited.add(start); qu.add(start); while (qu.size() > 0){ String v = qu.remove(); for(String adj : myGraph.getAdjacent(v)){ if (! visited.contains(adj)) { visited.add(adj); qu.add(adj); } return visited; } 16CompSci 100e, Spring 2011

DFS arrives public Set dfs(String start){ Set visited = new TreeSet (); Stack qu = new Stack (); visited.add(start); qu.push(start); while (qu.size() > 0){ String v = qu.pop(); for(String adj : myGraph.getAdjacent(v)){ if (! visited.contains(adj)) { visited.add(adj); qu.push(adj); } return visited; } 17CompSci 100e, Spring 2011

What is the Internet? The Internet was originally designed as an "overlay" network running on top of existing phone and other networks. It is based on a small set of software protocols that direct routers inside the network to forward data from source to destination, while applications run on the Internet to rapidly scale into a critical global service. However, this success now makes it difficult to create and test new ways of protecting it from abuses, or from implementing innovative applications and services. 18CompSci 100e, Spring 2011

Graph implementations Typical operations on graph: – Add vertex – Add edge (parameters?) – getAdjacent(vertex) – getVertices(..) – String->Vertex (vice versa) Different kinds of graphs – Lots of vertices, few edges, sparse graph Use adjacency list – Lots of edges (max # ?) dense graph Use adjacency matrix Adjacency list

Graph implementations (continued) Adjacency matrix – Every possible edge represented, how many? Adjacency list uses O(V+E) space – What about matrix? – Which is better? What do we do to get adjacent vertices for given vertex? – What is complexity? – Compared to adjacency list? What about weighted edges? TF …

Shortest path in weighted graph We need to modify approach slightly for weighted graph – Edges have weights, breadth first doesn’t work – What’s shortest path from A to F in graph below? Use same idea as breadth first search – Don’t add 1 to current distance, add ??? – Might adjust distances more than once – What vertex do we visit next? What vertex is next is key – Use greedy algorithm: closest – Huffman is greedy, … D E A B C F CompSci 100e, Spring 2011

What about connected components? What computers are reachable from this one? What people are reachable from me via acquaintanceship? – Start at some vertex, depth-first search (breadth?) Mark nodes visited – Repeat from unvisited vertex until all visited What is minimal size of a component? Maximal size? – What is complexity of algorithm in terms of V and E? What algorithms does this lead to in graphs? 22CompSci 100e, Spring 2011

Greedy Algorithms Reviewed A greedy algorithm makes a locally optimal decision that leads to a globally optimal solution – Huffman: choose minimal weight nodes, combine Leads to optimal coding, optimal Huffman tree – Making change with American coins: choose largest coin possible as many times as possible Change for $0.63, change for $0.32 What if we’re out of nickels, change for $0.32? Greedy doesn’t always work, but it does sometimes Weighted shortest path algorithm is Dijkstra’s algorithm, greedy and uses priority queue 23CompSci 100e, Spring 2011

Edsger Dijkstra Turing Award, 1972 Algol-60 programming language Goto considered harmful Shortest path algorithm Structured programming “Program testing can show the presence of bugs, but never their absence” For me, the first challenge for computing science is to discover how to maintain order in a finite, but very large, discrete universe that is intricately intertwined. And a second, but not less important challenge is how to mould what you have achieved in solving the first problem, into a teachable discipline: it does not suffice to hone your own intellect (that will join you in your grave), you must teach others how to hone theirs. The more you concentrate on these two challenges, the clearer you will see that they are only two sides of the same coin: teaching yourself is discovering what is teachable EWD 709EWD 709

Dijkstra’s Shortest Path Algorithm Similar to breadth first search, but uses a priority queue instead of a queue. Code below is for breadth first search (distance[] replaces set) Vertex cur = q.remove(); for(Vertex v : adjacent(cur)){ if (!visited.contains(v)){ // if distance[v] == INFINITY visited.add(v); // distance[v] = distance[cur]+1 q.add(v); } Dijkstra: Find minimal unvisited node, recalculate costs through node Vertex cur = pq.remove(); for(Vertex v : adjacent(cur)) if (distance[cur] + graph.weight(cur,v) < distance[v]) { distance[v] = distance[cur] + graph.weight(cur,v); pq.add(v); } 25CompSci 100e, Spring 2011

Shortest paths, more details Single-source shortest path – Start at some vertex S – Find shortest path to every reachable vertex from S A set of vertices is processed – Initially just S is processed – Each pass processes a vertex After each pass, shortest path from S to any vertex using just vertices from processed set (except for last vertex) is always known Next processed vertex is closest to S still needing processing SAB CE process B SAB CE E 7 7 S A B C process C E 7 7 S A B C

Dijkstra’s algorithm works (greedily) Choosing minimal unseen vertex to process leads to shortest paths Vertex cur = pq.remove(); for(Vertex v : adjacent(cur)) if (distance[cur]+graph.weight(cur,v) < distance[v]){ distance[v] = distance[cur] + graph.weight(cur,v); pq.add(v); } We always know shortest path through processed vertices – When we choose w, there can’t be a shorter path to w than distance[w] – it would go through processed u, we would have chosen u instead of w 27CompSci 100e, Spring 2011

Shafi Goldwasser RCS professor of computer science at MIT – Twice Godel Prize winner – Grace Murray Hopper Award – National Academy – Co-inventor of zero-knowledge proof protocols How do you convince someone that you know [a secret] without revealing the knowledge? Honesty and Privacy Work on what you like, what feels right, I know of no other way to end up doing creative work