470 likes | 635 Views
Graphs. Chapter 14. Simple Graph. a pair (V,E) V finite set of vertices points V = {a,b,c,d,e} E irreflexive, symmetric relation on V edges between vertices {{a,b},{b,c},{c,d},{d,e},{a,e}}. Directed graph. a pair (V,E) V finite set of vertices (points) E is an relation on V
E N D
Graphs Chapter 14
Simple Graph • a pair (V,E) • V • finite set of vertices • points • V = {a,b,c,d,e} • E • irreflexive, symmetric relation on V • edges between vertices • {{a,b},{b,c},{c,d},{d,e},{a,e}}
Directed graph • a pair (V,E) • V • finite set of vertices (points) • E is an relation on V • For any pair u,v in V, • E may contain one edge (u,v) from u to v, and (v,u) another edge from v to u. • see p. 432
Adjacent, Incident • Vertices u and v in undirected graph G are called adjacent if {u,v} is an edge of G • If edge e = {u,v}, e is called incidentwith vertices u and v • Edge econnectsu and v
Directed Edges • A directed edge (u,v) • shows a path from point u to point v • includes an arrow • u is called the initial vertex of (u,v) • v is called the terminal vertex of (u,v)
Abstract Graph Graph Vertices Edges Create an empty graph Insert a vertex Insert an edge Delete a vertex Delete an edge Determine if graph is empty Determine all vertices adjacent to a given vertex
Adjacency List • A graph representation • which specifies all vertices that are adjacent • to each vertex of the graph See digraph.h & Figure14-1.cpp
Example Digraph A B D E C F G
B F D F A E G F C G B Adjacency List Example A B C D E
Adjacency Matrix AG • A graph representation • A n´n zero-one matrix • A = [aij] See graph.h & graphtst.cpp
Example Digraph A B D E C F G
Adjacency Matrix Example A B C D E F G A B C D E F G
Depth-First Search http://www.cs.bme.hu/~gsala/alg_anims/3/graph-e.html
Perform DFS from A A B D E C F G
Breadth-First Search http://www.cs.bme.hu/~gsala/alg_anims/3/graph2-e.html
Perform BFS from A A B D E C F G
Shortest Path Algorithms • Algorithms exist • to find the shortest path • between two vertices (Dijkstra) • http://www.dgp.toronto.edu/people/JamesStewart/270/9798s/Laffra/DijkstraApplet.html
Shortest Unweighted Path • Performs a breadth-first processing of the nodes, assigning each of them a cost which is the length of the path from the starting node to the node • Uses a Queue of vertices to be examined • Picking a vertex is O(|V|)
1 function Dijkstra(Graph, source): 2 for each vertex v in Graph: // Initializations 3 dist[v] := infinity // Unknown distance function from source to v 4 previous[v] := undefined // Previous node in optimal path from source 5 dist[source] := 0 // Distance from source to source 6 Q := the set of all nodes in Graph// All nodes in the graph are unoptimized - thus are in Q 7 whileQis not empty: // The main loop 8 u := node in Q with smallest dist[] 9 remove u from Q 10 for each neighbor v of u: // where v has not yet been removed from Q. 11 alt := dist[u] + dist_between(u, v) // be careful in 1st step - dist[u] is infinity yet 12 ifalt < dist[v] // Relax (u,v) 13 dist[v] := alt 14 previous[v] := u 15 return previous[]
Topological Ordering • In a directed acyclic graph, • a topological ordering • is an ordering of vertices such that • if there is a path from u to v, • then v appears after u in the ordering • Example use – • prerequisite structure of classes
Topological Ordering There are many problems where we can easily say that one task has to be done before another, but it is harder to work out what order to do a whole bunch of such things. Directed edges occur when one node depends on the other, because of prerequisite relationships among courses or dependencies among nodes. The problem in both is to find an acceptable ordering of the nodes satisfying the dependencies. This is referred to as a topological ordering.
Topological Order - 1 • Start at CSI CSI CSII CSIII CS2813 CS4833 Discrete Math Analysis Algo. CSI
Topological Order - 2 • Remove CSI, now can take CSII or CS 2813 CSII CSIII CS2813 CS4833 CSI, CSII, CS 2813 or CSI, CS2813, CSII
Topological Order - 3 • Remove CSII, now take CS 2813 or CSIII CSIII CS2813 CS4833 CSI, CSII, CS 2813 or CSI, CSII, CSIII
Topological Order - 4 • Remove CSIII, now can take CS2813 CS2813 CS4833 CSI, CSII, CSIII, CS 2813
Topological Order - 5 • Remove CS 2813, now take CS 4833 CS4833 CSI, CSII, CSIII, CS 2813, CS 4833
Several Topological Orderings CSI CSII CSIII CS2813 CS4833 CSI, CS 2813, CSII, CS 4833, CSIII or CS I, CS 2813, CSII, CSIII, CS 4833 or CSI, CSII, CS 2813, CS 4833, CSIII etc.. . . . . . .
Topological Ordering The same problem arises in spreadsheets. Cell Contents Value 1 100 100 2 (Cell 1) + 10 110 3 (Cell 1) * 2 200 4 (Cell 2) + (Cell 3) 310 In the above example, if you change the value in cell 1, you need to first recalculate the values in cell 2 and 3, and only when that is done can you recalculate the value in cell 4.
1 2 4 3 Cell Contents Value 1 100 100 2 (Cell 1) + 10 110 3 (Cell 1) * 2 200 4 (Cell 2) + (Cell 3) 310
Topological Sorting • A topological sort finds a topological ordering • Algorithm: • Define the indegree of a vertex, v, as the number of edges (u,v) in the graph • If the graph is acyclic, there must be at least one vertex with indegree of 0
Topological Sorting Algo. • Take all vertices with indegree 0 and put on a queue • Remove the first vertex from the queue, “remove” it from the graph, and output • Put any new vertices with degree zero on the queue • Repeat last two steps until all vertices have been visited
Topo Sort Algorithm L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S insert n into L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then output error message (graph has at least one cycle) else output message (proposed topologically sorted order: L)
1 2 4 3 Cell Contents Value 1 100 100 2 (Cell 1) + 10 110 3 (Cell 1) * 2 200 4 (Cell 2) + (Cell 3) 310 Topological Ordering: 1 -> 2 -> 3 -> 4 OE 1 -> 3 -> 2 -> 4