Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Objects and Connections Many problems are naturally formulated in terms of objects and the connections between them. For example: Airline Routes--What is the fastest (or cheapest) way to get from one city to another? Electrical circuits--Circuit elements are wired together. How does the current flow? Job Scheduling--The objects are tasks that need to be performed. The connections indicate which jobs should be done before which other jobs. Web site design--How are the pages in the site connected? A graph is a mathematical object that describes such situations. Algorithms for graphs are fundamental to CS Graph Theory is a major branch of combinatorial mathematics.
Graphs A graph is a collection of vertices, V, and edges, E. An edge connects two vertices. a d a is the same as: d b c b c Vertices: a, b, c, d Edges: ab, bc, ac, ad
Definitions A path from one vertex to another is a list of vertices in which successive vertices are connected by edges in the graph. a d Example: A path from a to c could be ac or abc. b c A graph is connected if there is a path from every node to every other node in the graph. The above graph is connected. a d Not Connected e b c
More definitions A simple path is a path with no vertex repeated. A simple cycle is a simple path except the first and last vertex is repeated and, for an undirected graph, number of vertices >= 3. Example: abca is a cycle a d b c A tree is a graph with no cycles. a d b c
Definitions Continued A complete graph is a graph in which all edges are present. A sparse graph is a graph with relatively few edges. A dense graph is a graph with many edges. a d a d a d b c b c b c Complete Dense Sparse
Types of Graphs An undirected graph has no specific direction between the vertices. A directed graph has edges that are "one way". We can go from one vertex to another, but not vice versa. A weighted graph has weights associated with each edge. The weights can represent distances, costs, etc. 34 a d a d a d 22 4 13 19 b c b c b c Undirected Directed Weighted
Representing Graphs as an Adjacency List An adjacency list is an array which contains a list for each vertex. The list for a given vertex contains all the other vertices that are connected to the first vertex by a single edge. List 1 2 1 3 2 5 4 3 4 5
Representing a Graph with an Adjacency Matrix An adjacency matrix is a matrix with a row and column for each vertex. The matrix entry is 1 if there is an edge between the row vertex and the column vertex. 1 2 1 2 3 4 5 1 2 3 4 5 3 5 4 The diagonal may be zero or one. Each edge is represented twice.
Representing Directed Graphs In directed graphs we only include in the list those vertices that are pointed to by a given vertex. List 1 2 3 1 2 4 5 6 3 1 2 3 4 5 6 1 2 3 4 5 6 4 5 6
Representing Weighted Graphs In weighted graphs we include the weights of each edge. 2 List 1 2 3 1 1 3 6 5 7 2 4 4 5 6 1 3 1 2 3 4 5 6 1 2 3 4 5 6 4 5 6
Lists vs. Matrices Speed: An adjacency matrix provides the fastest way to determine whether or not a particular edge is present in the graph. For an adjacency list, there is no faster way than to search the entire list for the presence of the edge. Memory: Normally, an adjacency matrix requires more space (n2 entries). However, if n is small and the graph is unweighted, an adjacency matrix only needs 1 bit per entry. The adjacency list requires at least 1 word per entry (for the address of the next node of the list). This may offset the advantage of fewer entries.
Breadth First Search (BFS) Problem: Given a graph, G = (V, E), find all the vertices reachable from some source, s. Compute the distance (shortest path) from s to each vertex. Produce a tree such that: s is the root of the tree The nodes of the tree represent all reachable vertices The path from s to v corresponds to the path containing the fewest edges from s to v in the graph.
Strategy for BFS As we search the graph we: Keep track of "discovered" and "undiscovered" vertices. Maintain a frontier of discovered vertices that spreads out across the graph. Use three colors: White = undiscovered vertex Gray= Discovered vertex, but we don't know all its connections Black= Discovered vertex and we know all its connections.
Strategy continued For each gray vertex starting with s: Find all undiscovered vertices reachable by 1 edge from that vertex and color them gray. Add them to the tree below v. Color the original vertex black. Process all vertices at level k (distance k from s) before processing the vertices at level k+1
Pseudocode for BFS BFS(G, s) //G is graph, s is source vertex for each vertex u in G, V – {s} //Initialize each vertex to white u.color = white u.dist = ∞ u.pred = NIL s.color = gray //s is discovered s.dist = 0 s.pred = NIL Q = {} Q = Enqueue(Q, s) //Start with s in the queue while Q is not empty u = Dequeue(Q) //Process next element for each v in G.Adj[u] //Check all adjacent vertices if v.color == white //If undiscovered discover it v.color = gray v.dist = u.dist + 1 v.pred = u Enqueue(Q, v) //Add it to the queue u.color = black //Once adjacent vertices discovered, color this one black
Example r s t u v w x y We will work through this in class.
Analysis Initialization: Q(V) While loop: Each vertex enqueued and dequeued once: Q(V) Adjacency list scanned once for each vertex. Each edge is represented twice in all lists. Sum of the lengths of all lists = 2E, so The time to process the adjacency lists will be Q(E) Total for while loop = Q(V) + Q(E) = Q(V + E) Total = Q(V) + Q(V+E) = Q(V+E)