<<

Graphs: Structures and Vocabulary

z How do packets of bits/information get routed on the internet z Graphs are collections of vertices and edges Ê Message divided into packets on client (your) machine Ê Vertex is sometimes called a node Ê Packets sent out using routing tables toward destination Ê An edge connects two vertices • Packets may take different routes to destination • Direction is sometimes important, other times not so • What happens if packets lost or arrive out-of-order? • Sometimes edge has a weight/cost associated with it Ê Routing tables store local information, not global (why?)

z A sequence of vertices v0, v1, v2, …, vn-1 is a path where vk and z What about The Oracle of Bacon, Six Degrees of Separation, vk+1 are connected by an edge. Erdos Numbers, and Word Ladders? Ê If some vertex is repeated, the path is a cycle Ê All can be modeled using graphs Ê Trees are cycle-free (acyclic) graphs with a root Ê What kind of connectivity does each concept model? Ê A graph is connected if there is a path between any pair of vertices • Non-connected graphs have connected components z Graphs are everywhere in the world of algorithms (world?)

CPS 100 10.1 CPS 100 10.2

2 Graph Traversals 3 Pseudo-code for depth-first search 1 6 z Connected? void depthfirst(const string& vertex) Ê Why? // post: depth-first search from vertex complete 4 Ê Indegrees? Outdegrees? { if (! alreadySeen(vertex)) 7 { z Starting at 7 where can we get? 5 markAsSeen(vertex); Ê Depth-first search, envision each vertex as a room, with doors cout << vertex << endl; leading out for(each v adjacent to vertex) { • Go into a room, choose a door, mark the door and go out depthfirst(v); • Don’t go into a room you’ve already been in } • Backtrack when all doors marked and open next unopened door } Ê Rooms are stacked up, backtracking is really recursion } Ê One alternative uses a queue: breadth-first search z Clones are stacked up, problem? When are all doors out of vertex opened and visited? Can we make use of stack explicit?

CPS 100 10.3 CPS 100 10.4 Graph implementations Graph implementations (continued)

z Typical operations on graph: z Adjacency matrix Ê Add vertex Ê Every possible edge Ê Add edge (parameters?) represented, how many? Ê AdjacentVerts(vertex) z Adjacency list uses O(V+E) space Ê AllVerts(..) Ê What about matrix? Ê String->int (vice versa) Ê Which is better? TF …

z Different kinds of graphs z What do we do to get adjacent vertices for given vertex? Ê Lots of vertices, few edges, sparse graph Ê What is complexity? • Use adjacency list Ê Compared to adjacency list? Adjacency list Ê Lots of edges (max # ?) z What about weighted edges? • Use adjacency matrix

CPS 100 10.5 CPS 100 10.6

Other graph questions/operations Breadth first search

z What vertices are reachable from a given vertex z In an unweighted graph this finds the shortest path between a Ê Can depth-first search help here? start vertex and every vertex Ê Visit every node one away from start z What vertex has the highest in-degree (out-degree)? Ê Visit every node two away from start Ê How can we use a map to answer this question? • This is every node one away from a node one away Ê Visit every node three away from start z Shortest path between any two vertices Ê Breadth first search is storage expensive z Like depth first search, but use a queue instead of a stack Ê Dijkstra’s will offer an alternative, uses a priority queue too! Ê What features of a queue ensure shortest path? Ê Stack can be simulated with recursion, advantages? z Longest path in a graph Ê How many vertices on the stack/queue? Ê No known efficient algorithm

CPS 100 10.7 CPS 100 10.8 Pseudocode for breadth first What about word ladders

void breadthfirst(const string& vertex) z Find a path from white->house changing one letter // post: breadth-first search from vertex complete { Ê Real world? Computer vs. human? tqueue q; q.enqueue(vertex); • white write writs waits warts parts ports forts forte while (q.size() > 0) { • … rouse house q.dequeue(current); Ê See ladder.cpp program for(each v adjacent to current) { if (distance[v] == INFINITY) // not seen z How is this a graph problem? What are vertices/edges? { distance[v] = distance[current] + 1; z What about spell-checking, how is it similar? q.enqueue(v); Ê Edge from accomodate to accommodate } } Ê Can also use tries with wild-cards, e.g., acc*date } }

CPS 100 10.9 CPS 100 10.10

What about connected components? Shortest path in weighted graph

z What computers are reachable from this one? What people are z We need to modify approach slightly for weighted graph reachable from me via acquaintanceship? Ê Edges have weights, breadth first by itself doesn’t work Ê Start at some vertex, depth-first search (why not breadth)? Ê What’s shortest path from A to F in graph below? • Mark nodes visited

Ê Repeat, starting from an unvisited vertex (until all visited) z Use same idea as breadth first search Ê Don’t add 1 to current distance, add ??? z What is minimal size of a component? Maximal size? Ê Might adjust distances more than once 3 4 Ê What is complexity of algorithm in terms of V and E? Ê What vertex do we visit next? E 6 A 8 2 z What algorithms does this lead to in graphs? z What vertex is next is key D 3 4 Ê Use greedy algorithm: closest 2 F Ê Huffman is greedy, … B

CPS 100 10.11 CPS 100 10.12 Greedy Algorithms Edsger Dijkstra

z A greedy algorithm makes a locally optimal decision that z Turing Award, 1972 leads to a globally optimal solution z Operating systems and Ê Huffman: choose two nodes with minimal weight, concurrency combine z Algol-60 • Leads to optimal coding, optimal Huffman z Goto considered harmful Ê Making change with American coins: choose largest coin z Shortest path algorithm possible as many times as possible z Structured programming • Change for $0.63, change for $0.32 “Program testing can show the presence of bugs, but never their • What if we’re out of nickels, change for $0.32? absence” z A Discipline of programming z Greedy doesn’t always work, but it does sometimes “For the absence of a bibliography I z Weighted shortest path algorithm is Dijkstra’s algorithm, offer neither explanation nor greedy and uses priority queue apology”

CPS 100 10.13 CPS 100 10.14

Dijkstra’s Shortest Path Algorithm Shortest paths, more details A 4 z Similar to breadth first search, but uses a priority queue instead of a z Single-source shortest path E queue. Code below is for breadth first search 7 3 1 Ê Start at some vertex S 2 6 C q.dequeue(vertex w) Ê Find shortest path to every S foreach (vertex v adjacent to w) 3 7 reachable vertex from S 2 if (distance[v] == INT_MAX) // not visited SA BCE z A set of vertices is processed B

{ 8 distance[v] = distance[w] + 1; Ê Initially just S is processed 072 6 q.enqueue(v); process B } Ê Each pass processes a vertex 072 5 9 After each pass, shortest path from A 4 z Dijkstra: Find minimal unvisited node, recalculate costs through node S to any vertex using just vertices 7 3 1 E from processed set (except for last 2 q.deletemin(vertex w) vertex) is always known 6 C foreach (vertex v adjacent to w) S if (distance[w] + weight(w,v) < distance[v]) z Next processed vertex is closest 3 7SA B { 2 CE to S still needing processing B distance[v] = distance[w] + weight(w,v); 0 6 2 5 7 q.enqueue(vertex(v, distance[v])); } process C

CPS 100 10.15 CPS 100 10.16 Dijkstra’s algorithm works (greedily) Topological sort

z Choosing minimal unseen vertex to process leads to shortest z Given a paths (DAG) Ê Order vertices so that any if 0 1 2 3 4 5 6 q.deletemin(vertex w) there is an edge (v,w), then v foreach (vertex v adjacent to w) appears before w in the order if (distance[w] + weight(w,v) < distance[v]) { z Prerequisites for a major, take distance[v] = distance[w] + weight(w,v); CPS 100 before CPS 130 q.enqueue(vertex(v, distance[v])); } Ê Edge(cps100,cps130) 0 belt Ê Topological sort gives an ordering for taking courses underwear z We always know shortest path through processed vertices z Where does ordering start? 6 Ê When we choose w, there can’t be a shorter path to w than 1 shirt distance[w] – it would go through processed u, then we Ê First vertex has no prereqs pants 5 would have chosen u instead of w Ê “remove” this vertex, continue 3 4 Ê Depends on in-degree jacket shoes socks 2

CPS 100 10.17 CPS 100 10.18

Pseudocode for topological sort Minimum Spanning Trees

void topologicalsort(Graph G) z Minimum weight spanning tree (MST) properties { Ê Subgraph of a given undirected graph with edge weights ArbitrarySet fringe; count[v] = G.inDegree(v); Ê Contains all vertices of a given graph for ( i=0; i < G.vertexSize(); v++) Ê Minimizes the sum of its edge weights if ((count[v] = G.inDegree(i)) == 0) z How do we find the MST of a graph? fringe.add(v); // topological sort traversal while (!fringe.isEmpty()) { z Key insight: Vertex v1 = fringe.removeAny(); Ê If the vertices of a connected graph G are divided into two result.pushback(v1); disjoing non-empty sets G and G , then any MST for G will foreach edge w (v,w) { 0 1 count[w]--; contain one of the edges running between a vertex in G0 and a if (count[w] == 0) vertex in G1 that has minimal weight fringe.add(w); z Solution } Ê Prim’s algorithm } } Ê Kruskal’s algorithm

CPS 100 10.19 CPS 100 10.20