Graphs, the Internet, and Everything

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
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.
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.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
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.
CPS Graphs, the Internet, and Everything
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
COSC 2007 Data Structures II Chapter 14 Graphs III.
Representing and Using 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.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CompSci Graphs, the Internet, and Everything
CPS Graphs: Structures and Algorithms l How do packets of bits/information get routed on the internet  Message divided into packets on client.
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.
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 A New Data Structure
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
Lecture 11 Graph Algorithms
Fundamentals, Terminology, Traversal, Algorithms
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Cse 373 May 8th – Dijkstras.
C.Eng 213 Data Structures Graphs Fall Section 3.
Introduction to Graphs
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.
Short paths and spanning trees
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs Graph transversals.
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
CSE 373: Data Structures and Algorithms
Chapter 11 Graphs.
Connected Components, Directed Graphs, Topological Sort
ITEC 2620M Introduction to Data Structures
Chapter 15 Graphs © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSCI2100 Data Structures Tutorial
CSE 373: Data Structures and Algorithms
Chapter 16 1 – Graphs Graph Categories Strong Components
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
GRAPH – Definitions A graph G = (V, E) consists of
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:

Graphs, the Internet, and Everything http://www.caida.org/

Airline routes

Word ladder

Graphs: Structures and Algorithms 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?) What about The Oracle of Bacon, Erdos Numbers, and Word Ladders? All can be modeled using graphs What kind of connectivity does each concept model? Graphs are everywhere in the world of algorithms (world?)

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 v0, v1, …, vn-1 is a path where vk and vk+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 78 268 204 190 394 LGA $441 LAX $186 $412 $1701 ORD DCA $186

Graph questions/algorithms What vertices are reachable from a given vertex? Two standard traversals: depth-first, breadth-first Find connected components, groups of connected vertices Shortest path between any two vertices (weighted graphs?) Breadth first search is storage expensive Dijkstra’s algorithm is efficient, uses a priority queue too! Longest path in a graph No known efficient algorithm Visit all vertices without repeating? Visit all edges? With minimal cost? Hard!

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/vector/map for this Can keep useful information to help with visited status Order in which vertices visited can be important Storage and runtime efficiency of traversals important What other data structures do we have: stack, queue, … What happens when we traverse using priority queue?

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

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 every node one away from a node one away Visit every node three away from start, … Put vertex on queue to start (initially just one) Repeat: take vertex off queue, put all adjacent vertices on Don’t put a vertex on that’s already been visited (why?) When are 1-away vertices enqueued? 2-away? 3-away? How many vertices on queue?

Code for breadth first void breadthfirst(const string& vertex) 1 2 3 4 5 6 7 void breadthfirst(const string& vertex) // post: breadth-first search done { tset<string> visited; tqueue<string> q; q.enqueue(vertex); visited.insert(vertex); while (q.size() > 0) { string current; q.dequeue(current); // process current for(each v adjacent to current){ if (!visited.contains(v)){ // not visited visited.insert(v); q.enqueue(v); }

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

Depth first with stack/no recursion void depthfirst(const string& vertex) // post: depth-first search from vertex complete { tset<string> visited; stack<string> st; st.push(vertex); visited.insert(vertex); // mark this room while (st.size() > 0) { string current; st.pop(current); // process current for(each v adjacent to current){ if (!visited.contains(v)){ // not visited visited->insert(v); st.push(v); } 1 2 3 4 5 6 7

Depth and Breadth compared void breadth(const string& vertex) // post: breadth-first search done { tset<string> visited; tqueue<string> q; q.enqueue(vertex); visited.insert(vertex); while (q.size() > 0) { string current; q.dequeue(current); // process current for(v adjacent to current){ if (!visited.contains(v)){ visited.insert(v); q.enqueue(v); } void depth(const string& vertex) // post: depth-first search done { tset<string> visited; stack<string> st; st.push(vertex); visited.insert(vertex); while (st.size() > 0) { string current; st.pop(current); // process current for(v adjacent to current){ if (!visited.contains(v)){ visited->insert(v); st.push(v); }

Graph implementations Typical operations on graph: Add vertex Add edge (parameters?) AdjacentVerts(vertex) AllVerts(..) String->int (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? T F …

What about word ladders 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 ladderXXX.cpp programs How is this a graph problem? What are vertices/edges? What about spell-checking, how is it similar? Edge from accomodate to accommodate Can also use tries with wild-cards, e.g., acc*date

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 (why not breadth)? Mark nodes visited Repeat, starting from an 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?

Shortest path in weighted graph 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? 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 4 3 6 2 8

Greedy Algorithms 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? Greedy doesn’t always work, but it does sometimes Weighted shortest path algorithm is Dijkstra’s algorithm, greedy and uses priority queue

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

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 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); } Dijkstra: Find minimal unvisited node, recalculate costs through node q.deletemin(vertex w) if (distance[w] + weight(w,v) < distance[v]) distance[v] = distance[w] + weight(w,v); q.insert(vertex(v, distance[v]));

Shortest paths, more details 7 S A B C 6 2 4 3 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 1 S A B C E 7 2 6 8 process B 7 2 5 9 E 7 S A B C 6 2 4 3 3 1 S A B C E 6 2 5 7 process C

Dijkstra’s algorithm works (greedily) 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.insert(vertex(v, distance[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, then we would have chosen u instead of w

Greedy Algorithms Huffman compression is a greedy algorithm that works Where is "greed" used Dijkstra's algorithm is a greedy algorithm that works Which vertex visited? Prim's Minimal-spanning algorithm (see prim.cpp) works How is this algorithm greedy? 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?

Topological sort 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 Prerequisites for a major, take CPS 100 before CPS 130 Edge(cps100,cps130) Topological sort gives an ordering for taking courses Where does ordering start? First vertex has no prereqs “remove” this vertex, continue Depends on in-degree 0 1 2 3 4 5 6 belt underwear 6 shirt jacket 1 pants 5 4 3 shoes socks 2