18
Graphs

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

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

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

Graphs

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

OverviewWhat is a graph? Some terminologyTypes of graphImplementing graphs (briefly)Some graph algorithms

Graphs 2/18

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

Questions we can’t answer with linear structures or treesWhat if:The city wants to clean each street without driving down

any of them twice?

Graphs p. 3/18

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

Questions we can’t answer with linear structures or treesWhat 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

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

Questions we can’t answer with linear structures or treesWhat 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

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

Questions we can’t answer with linear structures or treesWhat 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

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

Some terminologyConsider 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

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

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

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

Types of graphFind 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

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

Undirected graphs: towards implementationGiven 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

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

Directed graphs: towards implementationGiven 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

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

Back to those questions we wanted to askWhat 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

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

GraphADTIf we had a GraphADT, it might have operations like:public Vector<Vertex> findEulerCircult(Vector<Vertex>);

public Vector<Vertex> travelingSalesman();

public Vector<Vertex> findPath(Vertex a, Vertex b);

public Vector<Vertex> findShortestPath(Vertex a, Vertex b);

Each of which solves one of the problems above.

Graphs p. 13/18

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

Finding an Euler CircuitThe 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

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

The Traveling Salesman ProblemTraveling 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

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

The Traveling Salesman ProblemTraveling 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 2n

An NP-complete problem.

Graphs p. 16/18

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

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 noneSome have more than oneOnly algorithm: trial and error

Graphs p. 17/18

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

Coming AttractionsNext 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