Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I.

Similar presentations


Presentation on theme: "1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I."— Presentation transcript:

1 1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I

2 2 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.

3 3 Graphs A graph is a collection of vertices, V, and edges, E. An edge connects two vertices. a d cb ad cb is the same as: Vertices: a, b, c, d Edges: ab, bc, ac, ad

4 4 Definitions A path from one vertex to another is a list of vertices in which successive vertices are connected by edges in the graph. ad cb Example: A path from a to c could be ac or abc. A graph is connected if there is a path from every node to every other node in the graph. The above graph is connected. ad cb e Not Connected

5 5 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. ad cb Example: abca is a cycle A tree is a graph with no cycles. ad cb

6 6 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. ad cb ad cb ad cb Complete DenseSparse

7 7 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. ad cb ad cb ad cb 34 22 13 19 4 UndirectedDirectedWeighted

8 8 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. 12 45 3 List 1 2 3 4 5

9 9 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. 12 45 3 1 2 3 4 5 1 2 3 4 5 The diagonal may be zero or one. Each edge is represented twice.

10 10 Representing Directed Graphs In directed graphs we only include in the list those vertices that are pointed to by a given vertex. 12 54 3 List 1 2 3 4 5 6 6 1 2 3 4 5 6 1 2 3 4 5 6

11 11 Representing Weighted Graphs In weighted graphs we include the weights of each edge. 12 54 3 List 1 2 3 4 5 6 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 7 1

12 12 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 (n 2 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.

13 13 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.

14 14 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.

15 15 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

16 Pseudocode for BFS BFS(G, s){G is graph, s is source vertex} for v in vertices(G) do{Initialize each vertex to white} color[v] <- white d[v] <- infinity parent[v] <- nil colors[s] <- gray{s is discovered} d[s] <- 0 parent[s] <- nil Q <- Enqueue(s, Empty-Queue){Start with s in the queue} while not Empty-Queue?(Q) do f <- Dequeue(Q){Process next element} for g in Adj[f] do{Check all adjacent vertices} if color[g] = white then{If undiscovered discover it} color[g] <- gray d[g] <- d[f] + 1 parent[g] <-f Enqueue(g, Q){Add it to the queue} color[f] <- black{Once adjacent vertices discovered, color this one black}

17 17 Example rstu yxwv We will work through this in class.

18 18 Analysis Initialization:  (V) While loop: Each vertex enqueued and dequeued once:  (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  (E) Total for while loop =  (V) +  (E) =  (V + E) Total =  (V) +  (V+E) =  (V+E)


Download ppt "1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I."

Similar presentations


Ads by Google