Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Chapter 20.

Similar presentations


Presentation on theme: "Graphs Chapter 20."— Presentation transcript:

1 Graphs Chapter 20

2 FIGURE 20-1 An ordinary line graph
Terminology Graphs represent relations among data items G = { V, E} A graph is a set of vertices (nodes) and A set of edges that connect the vertices FIGURE 20-1 An ordinary line graph

3 Terminology FIGURE 20-2 A graph and one of its subgraphs

4 Terminology FIGURE 20-3 Examples of undirected graphs that are either connected, disconnected, or complete

5 Terminology FIGURE 20-4 Graph-like structures that are not graphs

6 Terminology FIGURE 20-5 Examples of two kinds of graphs – an undirected weighted graph

7 Terminology FIGURE 20-5 Examples of two kinds of graphs –a directed weighted graph

8 Graphs as ADTs What data structure would you use? What operations should be supplied?

9 Graphs as ADTs ADT graph operations Test if empty
Get number of vertices, edges in a graph See if edge exists between two given vertices Add vertex to graph whose vertices have distinct, different values from new vertex Add/remove edge between two given vertices Remove vertex, edges to other vertices Retrieve vertex that contains given value

10 Graphs as ADTs- Abstract Interface
LISTING 20-1 A C++ interface for undirected, connected graphs

11 Graphs as ADTs LISTING 20-1 A C++ interface for undirected, connected graphs

12 Graphs as ADTs LISTING 20-1 A C++ interface for undirected, connected graphs

13 Implementing Graphs FIGURE 20-7 (a) A directed unweighted graph and (b) its adjacency matrix

14 Implementing Graphs FIGURE 20-8 (a) A weighted undirected graph and (b) its adjacency matrix

15 Implementing Graphs FIGURE 20-9 (a) A directed graph and (b) its adjacency list

16 Implementing Graphs FIGURE (a) A weighted undirected graph and (b) its adjacency list

17 Implementing Graphs Adjacency list Adjacency matrix
Often requires less space than adjacency matrix Supports finding vertices adjacent to given vertex Adjacency matrix Most easily supports process of seeing if there exists an edge from vertex i to vertex j

18 Graph Traversals Visits all vertices it can reach
Visits all vertices if and only if the graph is connected Connected component Subset of vertices visited during a traversal that begins at a given vertex

19 Depth-First Search DFS traversal
Goes as far as possible from a vertex before backing up Recursive traversal algorithm or stack based algorithm

20 Breadth-First Search BFS traversal
Visits all vertices adjacent to a vertex before going forward BFS is a first visited, first explored strategy uses a queue based strategy Contrast DFS as last visited, first explored

21 Depth-First Search FIGURE The results of a depth-first traversal, beginning at vertex a, of the graph in Figure 20-12

22 Breadth-First Search FIGURE The results of a breadth-first traversal, beginning at vertex a, of the graph in Figure 20-12

23 Applications of Graphs
Topological Sorting – finding an order of precedence relationships Spanning Tree – find a connected components Minimum Spanning Trees – find a minimum weight connected component Shortest Paths – find the shortest path from source to destination, find all pairs shortest paths Circuits – does a graph have a cycle Some Difficult Problems – Traveling Salesman Problem, Hamiltonian Circuit Problem, Euler Circuit Problem, Finding a k-Clique in a graph, etc…..

24 Topological Sorting FIGURE A directed graph without cycles

25 Topological Sorting FIGURE The graph in Figure arranged according to two topological orders

26 Topological Sorting

27 Topological Sorting This is an incorrect DFS! Why?
FIGURE A trace of topSort2 for the graph in Figure using depth-first search

28 Spanning Trees A tree is an undirected connected graph without cycles
Detecting a cycle in an undirected graph Connected undirected graph with n vertices must have at least n – 1 edges If it has exactly n – 1 edges, it cannot contain a cycle With more than n – 1 edges, must contain at least one cycle

29 Spanning Trees FIGURE A spanning tree for the graph in Figure Dotted edges not in spanning tree.

30 Spanning Trees FIGURE Connected graphs that each have four vertices and three edges

31 Spanning Trees Another possible spanning tree for the graph in Figure

32 Spanning Trees FIGURE The BFS spanning tree rooted at vertex a for the graph in Figure 20-12

33 Minimum Spanning Trees
FIGURE A weighted, connected, undirected graph

34 Minimum Spanning Trees
FIGURE A trace of primsAlgorithm for the graph in Figure 20-23, beginning at vertex a

35 Minimum Spanning Trees
FIGURE A trace of primsAlgorithm for the graph in Figure 20-23, beginning at vertex a

36 Minimum Spanning Trees
FIGURE A trace of primsAlgorithm for the graph in Figure 20-23, beginning at vertex a

37 Shortest Paths The shortest path between two vertices in a weighted graph Has the smallest edge-weight sum FIGURE (a) A weighted directed graph and (b) its adjacency matrix

38 Shortest Paths FIGURE A trace of the shortest-path algorithm applied to the graph in Figure 20-25a

39 Shortest Paths FIGURE Checking weight[u] by examining the graph: (a) weight[2] in step 2; (b) weight[1] in step 3;

40 Shortest Paths FIGURE Checking weight[u] by examining the graph: (c) weight[3] in step 3; (d) weight[3] in step 4

41 Circuits Circuit Another name for a type of cycle common in statement of certain types of problems Typical circuits either visit every vertex once or every edge once Euler Circuit – in P Begins at vertex v Passes through every edge exactly once Terminates at v Hamiltonian Circuit - in NP Complete Passes through every vertex exactly once

42 Circuits FIGURE (a) Euler’s bridge problem and (b) its multigraph representation

43 Circuits FIGURE Pencil and paper drawings

44 Circuits FIGURE Connected undirected graphs based on the drawings in Figure 20-29

45 Circuits FIGURE The steps to find an Euler circuit for the graph in Figure 20-30b

46 Circuits FIGURE The steps to find an Euler circuit for the graph in Figure 20-30b

47 Circuits FIGURE The steps to find an Euler circuit for the graph in Figure 20-30b

48

49 Some Difficult Problems
Hamilton circuit Begins at vertex v Passes through every vertex exactly once Terminates at v Variation is “traveling salesperson problem” Visit every city on his route exactly once Edge (road) has associated cost (mileage) Goal is determine least expensive circuit

50 End Chapter 20


Download ppt "Graphs Chapter 20."

Similar presentations


Ads by Google