Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs. Overview What is a graph? Some terminology Types of graph Implementing graphs (briefly) Some graph algorithms Graphs 2/18.

Similar presentations


Presentation on theme: "Graphs. Overview What is a graph? Some terminology Types of graph Implementing graphs (briefly) Some graph algorithms Graphs 2/18."— Presentation transcript:

1 Graphs

2 Overview What is a graph? Some terminology Types of graph Implementing graphs (briefly) Some graph algorithms Graphs 2/18

3 Questions we can’t answer with linear structures or trees What if: The city wants to clean each street without driving down any of them twice? Graphs p. 3/18

4 Questions we can’t answer with linear structures or trees What if: The city wants to clean each street without driving down any of them twice? A trucking company wants to make deliveries to a set of locations without visiting the same location twice? Graphs p. 4/18

5 Questions we can’t answer with linear structures or trees What if: The city wants to clean each street without driving down any of them twice? A trucking company wants to make deliveries to a set of locations without visiting the same location twice? We want to find the (shortest) path between two people on Facebook, going from friend to friend? Graphs p. 5/18

6 Questions we can’t answer with linear structures or trees What if: The city wants to clean each street without driving down any of them twice? A trucking company wants to make deliveries to a set of locations without visiting the same location twice? We want to find the (shortest) path between two people on Facebook, going from friend to friend? We want to find the shortest airplane trip connecting a set of cities? The cheapest? Graphs p. 6/18

7 Some terminology Consider Figures 13.1 and 13.2 (pp. 378, 389). List the vertices. List the edges. Give two pairs of adjacent vertices. What are the neighbors of vertex D in each graph? What would we need to do to make Figure 13.1 a complete graph? Explain. If there are n vertices, how many edges are needed to make a complete graph? Explain. Graphs p. 7/18

8 Terminology (continued) Still with regard to Figures 13.1 and 13.2: Give two paths from A to D in Figure 13.1, one of which involves visiting the same vertex at least twice. Are the two graphs connected? Why or why not? Is there a cycle in Figure 13.1? If yes, list the vertices on the cycle in order. If no, why not? Name a type of graph that we have seen that has no cycles and explain why. Graphs p. 8/18

9 Types of graph Find an example of an undirected graph in your book. Explain. Find an example of a directed graph (aka “digraph”). Explain. Find an example of a weighted graph. Explain. Graphs p. 9/18

10 Undirected graphs: towards implementation Given the undirected graph with edges ((A,B), (B,C), (C, D), (A, C)) Draw the graph. For each vertex, give its adjacency list. Draw the adjacency matrix (2D array) corresponding to the graph. Graphs p. 10/18

11 Directed graphs: towards implementation Given the directed graph with edges ((A,B), (B,C), (C, D), (A, C)) Draw the graph. For each vertex, give its adjacency list. Draw the adjacency matrix (2D array) corresponding to the graph. Graphs p. 11/18

12 Back to those questions we wanted to ask What are the vertices and edges if: The city wants to clean each street without driving down any of them twice? A trucking company wants to make each delivery without visiting the same location twice? We want to find if the (shortest) path between two people on Facebook, going from friend to friend? We want to find the shortest airplane trip connecting a sets of cities? The cheapest? Graphs p. 12/18

13 GraphADT If we had a GraphADT, it might have operations like: public Vector findEulerCircult(Vector ); public Vector travelingSalesman(); public Vector findPath(Vertex a, Vertex b); public Vector findShortestPath(Vertex a, Vertex b); Each of which solves one of the problems above. Graphs p. 13/18

14 Finding an Euler Circuit The Euler Circuit problem: cross all edges without repeats. Algorithm: If (some vertex has an odd number of edges) return null. Start with any vertex. Follow edges in any direction EXCEPT never “burn bridges.” Try this out with the following graph: ((A, B), (B, D), (D, C), (C, A), (D, F), (F, E), (E, C), (F, H), (H, G), (G, E), (E, N), (N, C), (D, I), (I, J), (J, F), (I, L), (L, J), (I, K), (K, L), (L, M), (M, J)) Graphs p. 14/18

15 The Traveling Salesman Problem Traveling Salesman (a famous problem in computer science): given a weighted graph, find the “cheapest” path that visits each vertex exactly once and returns back to the start. Big Oh? Graphs p. 15/18

16 The Traveling Salesman Problem Traveling Salesman (a famous problem in computer science): given a weighted graph, find the “cheapest” path that visits each vertex exactly once and returns back to the start. Big Oh? O(n!) -- even worse than 2 n An NP-complete problem. Graphs p. 16/18

17 The Traveling Salesman Problem (simplified) A simpler version: find any path that visits each vertex once and returns to the start (a Hamiltonian circuit). Some graphs have none Some have more than one Only algorithm: trial and error Graphs p. 17/18

18 Coming Attractions Next time we’ll look at a new data structure called a heap that allows us to model situations like hospital emergency rooms (where you insert according to some priority but delete like a queue) Homework: read Chapter 11 Heaps and Priority Queues (or the equivalent in the earlier edition). Graphs p. 18/18


Download ppt "Graphs. Overview What is a graph? Some terminology Types of graph Implementing graphs (briefly) Some graph algorithms Graphs 2/18."

Similar presentations


Ads by Google