CPS 100 11.1 Graphs: Structures and Algorithms l How do packets of bits/information get routed on the internet  Message divided into packets on client.

Slides:



Advertisements
Similar presentations
§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.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CompSci 100e Program Design and Analysis II April 19, 2011 Prof. Rodger CompSci 100e, Spring
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
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.
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.
Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 CHAPTER 4 - PART 2 GRAPHS 1.
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.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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.
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.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
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.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
CPS Scoreboard l What else might we want to do with a data structure? l What are maps’ limitations? AlgorithmInsertionDeletionSearch Unsorted.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
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 100e 10.1 Is there a Science of Networks? l What kinds of networks are there? l From Bacon numbers to random graphs to Internet  From FOAF to.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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.
Graphs Representation, BFS, DFS
Graphs, the Internet, and Everything
Graphs, the Internet, and Everything
Ellen Walker CPSC 201 Data Structures Hiram College
Depth-First Search.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Graphs Representation, BFS, DFS
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 11 Graphs.
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI2100 Data Structures Tutorial
CSE 373: Data Structures and Algorithms
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

CPS Graphs: Structures and Algorithms l How do packets of bits/information get routed on the internet  Message divided into packets on client (your) machine  Packets sent out using routing tables toward destination Packets may take different routes to destination What happens if packets lost or arrive out-of-order?  Routing tables store local information, not global (why?) l What about The Oracle of Bacon, Six Degrees of Separation, Erdos Numbers, and Word Ladders?The Oracle of BaconSix Degrees of Separation Erdos Numbers  All can be modeled using graphs  What kind of connectivity does each concept model? l Graphs are everywhere in the world of algorithms (world?)

CPS Vocabulary l 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 l 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

CPS Vocabulary/Traversals l Connected?  Connected components? Weakly connected (directionless)  Indegrees? Outdegrees? l Starting at 7 where can we get?  Depth-first search, envision each vertex as a room, with doors leading out Go into a room, choose a door, mark the door and go out Don’t go into a room you’ve already been in Backtrack if all doors marked (to room with unmarked door)  Rooms are stacked up, backtracking is really recursion  One alternative uses a queue: breadth-first search

CPS Pseudo-code for depth-first search void depthfirst(const string& vertex) // post: depth-first search from vertex complete { if (! alreadySeen(vertex)) { markAsSeen(vertex); cout << vertex << endl; for(each v adjacent to vertex) { depthfirst(v); } l Clones are stacked up, problem? When are all doors out of vertex opened and visited? Can we make use of stack explicit?

CPS Other graph questions/operations l What vertices are reachable from a given vertex  Can depth-first search help here? l What vertex has the highest in-degree (out-degree)?  How can we use a map to answer this question? l Shortest path between any two vertices  Breadth first search is storage expensive  Dijkstra’s algorithm will offer an alternative, uses a priority queue too! l Longest path in a graph  No known efficient algorithm

CPS Breadth first search l 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 every node one away from a node one away  Visit every node three away from start l Like depth first search, but use a queue instead of a stack  What features of a queue ensure shortest path?  Stack can be simulated with recursion, advantages?  How many vertices on the stack/queue?

CPS Pseudocode for breadth first void breadthfirst(const string& vertex) // post: breadth-first search from vertex complete { tqueue q; q.enqueue(vertex); distance[vertex] = 0; // start somewhere while (q.size() > 0) { q.dequeue(current); for(each v adjacent to current) { if (distance[v] == INFINITY) // not seen { distance[v] = distance[current] + 1; q.enqueue(v); }

CPS Depth, Breadth, other traversals l We want to visit every vertex that can be reached from a specific starting vertex  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/vector for doing this  Order in which vertices visited can be important  Storage and runtime efficiency of traversals important l What other data structures do we have: stack, queue, …  What happens when we traverse using priority queue?

CPS Graph implementations l Typical operations on graph:  Add vertex  Add edge (parameters?)  AdjacentVerts(vertex)  AllVerts(..)  String->int (vice versa) l 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

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

CPS What about word ladders l Find a path from white->house changing one letter  Real world? Computer vs. human? white write writs waits warts parts ports forts forte … rouse house  See ladder.cpp program l How is this a graph problem? What are vertices/edges? l What about spell-checking, how is it similar?  Edge from accomodate to accommodate  Can also use tries with wild-cards, e.g., acc*date

CPS What about connected components? l What computers are reachable from this one? What people are reachable from me via acquaintanceship?  Start at some vertex, depth-first search (why not breadth)? Mark nodes visited  Repeat, starting from an unvisited vertex (until all visited) l What is minimal size of a component? Maximal size?  What is complexity of algorithm in terms of V and E? l What algorithms does this lead to in graphs?

CPS Shortest path in weighted graph l We need to modify approach slightly for weighted graph  Edges have weights, breadth first by itself doesn’t work  What’s shortest path from A to F in graph below? l 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? l What vertex is next is key  Use greedy algorithm: closest  Huffman is greedy, … D E A B C F

CPS Greedy Algorithms l A greedy algorithm makes a locally optimal decision that leads to a globally optimal solution  Huffman: choose two nodes with minimal weight, 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? l Greedy doesn’t always work, but it does sometimes l Weighted shortest path algorithm is Dijkstra’s algorithm, greedy and uses priority queue

CPS Edsger Dijkstra l Turing Award, 1972 l Operating systems and concurrency l Algol-60 programming language l Goto considered harmful l Shortest path algorithm l Structured programming “Program testing can show the presence of bugs, but never their absence” l A Discipline of programming “For the absence of a bibliography I offer neither explanation nor apology”

CPS Dijkstra’s Shortest Path Algorithm l Similar to breadth first search, but uses a priority queue instead of a queue. Code below is for breadth first search q.dequeue(vertex w) foreach (vertex v adjacent to w) if (distance[v] == INT_MAX) // not visited { distance[v] = distance[w] + 1; q.enqueue(v); } l Dijkstra: Find minimal unvisited node, recalculate costs through node q.deletemin(vertex w) foreach (vertex v adjacent to w) if (distance[w] + weight(w,v) < distance[v]) { distance[v] = distance[w] + weight(w,v); q.enqueue(vertex(v, distance[v])); }

CPS Shortest paths, more details l Single-source shortest path  Start at some vertex S  Find shortest path to every reachable vertex from S l 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 l 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

CPS Dijkstra’s algorithm works (greedily) l Choosing minimal unseen vertex to process leads to shortest paths q.deletemin(vertex w) foreach (vertex v adjacent to w) if (distance[w] + weight(w,v) < distance[v]) { distance[v] = distance[w] + weight(w,v); q.enqueue(vertex(v, distance[v])); } l 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, then we would have chosen u instead of w

CPS Greedy Algorithms l Huffman compression is a greedy algorithm that works  Where is "greed" used l Dijkstra's algorithm is a greedy algorithm that works  Which vertex visited? l Prim's Minimal-spanning algorithm (see prim.cpp) works  How is this algorithm greedy? l Making change in US is a greedy algorithm that works  Minimal coins for change of $0.75, $0.72, …  What if we don't have nickels: change for $0.32?

CPS Topological sort l Given a directed acyclic graph (DAG)  Order vertices so that any if there is an edge (v,w), then v appears before w in the order l Prerequisites for a major, take CPS 100 before CPS 130  Edge(cps100,cps130)  Topological sort gives an ordering for taking courses l Where does ordering start?  First vertex has no prereqs  “remove” this vertex, continue  Depends on in-degree socks underwear shoesbeltpantsshirt jacket