Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Python: From First Programs Through Data Structures Chapter 20 Graphs.

Similar presentations


Presentation on theme: "Fundamentals of Python: From First Programs Through Data Structures Chapter 20 Graphs."— Presentation transcript:

1 Fundamentals of Python: From First Programs Through Data Structures Chapter 20 Graphs

2 Fundamentals of Python: From First Programs Through Data Structures2 Objectives After completing this chapter, you will be able to: Use the relevant terminology to describe the difference between graphs and other types of collections Recognize applications for which graphs are appropriate Explain the structural differences between an adjacency matrix representation of a graph and the adjacency list representation of a graph

3 Fundamentals of Python: From First Programs Through Data Structures3 Objectives (continued) Analyze the performance of basic graph operations using the two representations of graphs Describe the differences between a depth-first traversal and a breadth-first traversal of a graph

4 4 Graphs: Definitions and Basic Properties  Imagine an organization that wants to set up teams of three to work on some projects.  In order to maximize the number of people on each team who had previous experience working together successfully, the director asked the members to provide names of their past partners.

5 5 This information is displayed below both in a table and in a diagram. Graphs: Definitions and Basic Properties

6 6 From the diagram, it is easy to see that Bev, Cai, and Flo are a group of three past partners, and so they should form one of these teams. The following figure shows the result when these three names are removed from the diagram. Graphs: Definitions and Basic Properties

7 7 This drawing shows that placing Hal on the same team as Ed would leave Gia and Ira on a team containing no past partners. However, if Hal is placed on a team with Gia and Ira, then the remaining team would consist of Ana, Dan, and Ed, and both teams would contain at least one pair of past partners. Drawings such as those shown previously are illustrations of a structure known as a graph. The dots are called vertices (plural of vertex) and the line segments joining vertices are called edges. Graphs: Definitions and Basic Properties

8 8 As you can see from the drawing, it is possible for two edges to cross at a point that is not a vertex. Note also that the type of graph described here is quite different from the “graph of an equation” or the “graph of a function.” In general, a graph consists of a set of vertices and a set of edges connecting various pairs of vertices. Graphs: Definitions and Basic Properties

9 9 Fundamentals of Python: From First Programs Through Data Structures 9 Graph Terminology Mathematically, a graph is a set V of vertices and a set E of edges, such that each edge in E connects two of the vertices in V We use node as a synonym for vertex

10 10 The edges may be straight or curved and should either connect one vertex to another or a vertex to itself, as shown below. Graphs: Definitions and Basic Properties

11 Fundamentals of Python: From First Programs Through Data Structures11 Graph Terminology (continued) One vertex is adjacent to another vertex if there is an edge connecting the two vertices (neighbors) Path: Sequence of edges that allows one vertex to be reached from another vertex in a graph

12 Fundamentals of Python: From First Programs Through Data Structures12 Graph Terminology (continued) A vertex is reachable from another vertex if and only if there is a path between the two Length of a path: Number of edges on the path Degree of a vertex: Number of edges connected to it –In a complete graph: Number of vertices minus one

13 13 When an edge connects a vertex to itself (as e 5 does), it is called a loop. When two edges connect the same pair of vertices (as e 2 and e 3 do), they are said to be parallel. It is quite possible for a vertex to be unconnected by an edge to any other vertex in the graph (as v 5 is), and in that case the vertex is said to be isolated. Graphs: Definitions and Basic Properties

14 Fundamentals of Python: From First Programs Through Data Structures14 Graph Terminology (continued) A graph is connected if there is a path from each vertex to every other vertex A graph is complete if there is an edge from each vertex to every other vertex

15 Fundamentals of Python: From First Programs Through Data Structures15 Graph Terminology (continued) Subgraph: Subset of the graph’s vertices and the edges connecting those vertices Connected component: Subgraph consisting of the set of vertices reachable from a given vertex

16 Fundamentals of Python: From First Programs Through Data Structures16 Graph Terminology (continued) Simple path: Path that does not pass through the same vertex more than once Cycle: Path that begins and ends at the same vertex

17 Fundamentals of Python: From First Programs Through Data Structures17 Graph Terminology (continued) Graphs can be undirected or directed (digraph) A directed edge has a source vertex and a destination vertex Edges emanating from a given source vertex are called its incident edges

18 Fundamentals of Python: From First Programs Through Data Structures18 Graph Terminology (continued) To convert undirected graph to equivalent directed graph, replace each edge in undirected graph with a pair of edges pointing in opposite directions

19 Fundamentals of Python: From First Programs Through Data Structures19 Graph Terminology (continued) A special case of digraph that contains no cycles is known as a directed acyclic graph, or DAG Lists and trees are special cases of directed graphs

20 Fundamentals of Python: From First Programs Through Data Structures20 Graph Terminology (continued) A dense graph has relatively many edges A sparse graph has relatively few edges The number of edges in a complete directed graph with N vertices is N * (N – 1) The number of edges in a complete undirected graph is N * (N – 1) / 2 The limiting case of a dense graph has approximately N 2 edges The limiting case of a sparse graph has approximately N edges

21 21 As noted earlier, a given pictorial representation uniquely determines a graph. However, a given graph may have more than one pictorial representation. Such things as the lengths or curvatures of the edges and the relative position of the vertices on the page may vary from one pictorial representation to another. Graphs: Definitions and Basic Properties

22 22 Examples of Graphs

23 23 Graphs are a powerful problem-solving tool because they enable us to represent a complex situation with a single image that can be analyzed both visually and with the aid of a computer. Examples of Graphs

24 Fundamentals of Python: From First Programs Through Data Structures24 Examples of Graphs Graphs serve as models of a wide range of objects: –A roadmap –A map of airline routes –A layout of an adventure game world –A schematic of the computers and connections that make up the Internet –The links between pages on the Web –The relationship between students and courses –A diagram of the flow capacities in a communications or transportation network

25 25 Using a Graph to Represent a Network Telephone, electric power, gas pipeline, and air transport systems can all be represented by graphs, as can computer networks—from small local area networks to the global Internet system that connects millions of computers worldwide. Questions that arise in the design of such systems involve choosing connecting edges to minimize cost, optimize a certain type of service, and so forth.

26 26 Example – Using a Graph to Represent a Network A typical network, called a hub and spoke model, is shown below. cont’d

27 Copyright © Cengage Learning. All rights reserved. Trails, Paths, and Circuits SECTION on

28 28 Trails, Paths, and Circuits The subject of graph theory began in the year 1736 when the great mathematician Leonhard Euler published a paper giving the solution to the following puzzle: The town of Königsberg in Prussia (now Kaliningrad in Russia) was built at a point where two branches of the Pregel River came together. It consisted of an island and some land along the river banks.

29 29 Trails, Paths, and Circuits These were connected by seven bridges as shown in Figure 10.2.1. The Seven Bridges of Königsberg Figure 10.2.1

30 30 Trails, Paths, and Circuits The question is this: Is it possible for a person to take a walk around town, starting and ending at the same location and crossing each of the seven bridges exactly once? To solve this puzzle, Euler translated it into a graph theory problem. He noticed that all points of a given land mass can be identified with each other since a person can travel from any one point to any other point of the same land mass without crossing a bridge.

31 31 Trails, Paths, and Circuits Thus for the purpose of solving the puzzle, the map of Königsberg can be identified with the graph shown in Figure 10.2.2, in which the vertices A, B, C, and D represent land masses and the seven edges represent the seven bridges. Figure 10.2.2 Graph Version of Königsberg Map

32 32 Trails, Paths, and Circuits In terms of this graph, the question becomes the following: Is it possible to find a route through the graph that starts and ends at some vertex, one of A, B, C, or D, and traverses each edge exactly once? Equivalently: Is it possible to trace this graph, starting and ending at the same point, without ever lifting your pencil from the paper?

33 33 Trails, Paths, and Circuits Take a few minutes to think about the question yourself. Can you find a route that meets the requirements? Try it! Looking for a route is frustrating because you continually find yourself at a vertex that does not have an unused edge on which to leave, while elsewhere there are unused edges that must still be traversed. If you start at vertex A, for example, each time you pass through vertex B, C, or D, you use up two edges because you arrive on one edge and depart on a different one.

34 34 So, if it is possible to find a route that uses all the edges of the graph and starts and ends at A, then the total umber of arrivals and departures from each vertex B, C, and D must be a multiple of 2. Or, in other words, the degrees of the vertices B, C, and D must be even. But they are not: deg(B) = 5, deg(C) = 3, and deg(D) = 3. Hence there is no route that solves the puzzle by starting and ending at A. Trails, Paths, and Circuits

35 35 Trails, Paths, and Circuits Similar reasoning can be used to show that there are no routes that solve the puzzle by starting and ending at B, C, or D. Therefore, it is impossible to travel all around the city crossing each bridge exactly once.

36 36 Definitions

37 37 Definitions Travel in a graph is accomplished by moving from one vertex to another along a sequence of adjacent edges. In the graph below, for instance, you can go from u 1 to u 4 by taking f 1 to u 2 and then f 7 to u 4. This is represented by writing u 1 f 1 u 2 f 7 u 4.

38 38 Definitions Or you could take the roundabout route Certain types of sequences of adjacent vertices and edges are of special importance in graph theory: those that do not have a repeated edge, those that do not have a repeated vertex, and those that start and end at the same vertex.

39 39 Definitions

40 40 Definitions For ease of reference, these definitions are summarized in the following table: Often a walk can be specified unambiguously by giving either a sequence of edges or a sequence of vertices.

41 41 Example 2 – Walks, Trails Paths, and Circuits In the graph below, determine which of the following walks are trails, paths, circuits, or simple circuits. a. b. c. d. e. f.

42 42 Example 2 – Solution a. This walk has a repeated vertex but does not have a repeated edge, so it is a trail from v 1 to v 4 but not a path. b. This is just a walk from v 1 to v 5. It is not a trail because it has a repeated edge. c. This walk starts and ends at v 2, contains at least one edge, and does not have a repeated edge, so it is a circuit. Since the vertex v 3 is repeated in the middle, it is not a simple circuit. d. This walk starts and ends at v 2, contains at least one edge, does not have a repeated edge, and does not have a repeated vertex. Thus it is a simple circuit.

43 43 Example 2 – Solution e. This is just a closed walk starting and ending at v 1. It is not a circuit because edge e 1 is repeated. f. The first vertex of this walk is the same as its last vertex, but it does not contain an edge, and so it is not a circuit. It is a closed walk from v 1 to v 1. (It is also a trail from v 1 to v 1.) cont’d

44 44 Definitions Because most of the major developments in graph theory have happened relatively recently and in a variety of different contexts, the terms used in the subject have not been standardized. For example, what this book calls a graph is sometimes called a multigraph, what this book calls a simple graph is sometimes called a graph, what this book calls a vertex is sometimes called a node, and what this book calls an edge is sometimes called an arc.

45 45 Definitions Similarly, instead of the word trail, the word path is sometimes used; instead of the word path, the words simple path are sometimes used; and instead of the words simple circuit, the word cycle is sometimes used. The terminology in this book is among the most common, but if you consult other sources, be sure to check their definitions.

46 46 Connectedness

47 47 Connectedness It is easy to understand the concept of connectedness on an intuitive level. Roughly speaking, a graph is connected if it is possible to travel from any vertex to any other vertex along a sequence of adjacent edges of the graph.

48 48 Connectedness The formal definition of connectedness is stated in terms of walks. If you take the negation of this definition, you will see that a graph G is not connected if, and only if, there are two vertices of G that are not connected by any walk.

49 49 Example 3 – Connected and Disconnected Graphs Which of the following graphs are connected?

50 50 Example 3 – Solution The graph represented in (a) is connected, whereas those of (b) and (c) are not. To understand why (c) is not connected, we know that in a drawing of a graph, two edges may cross at a point that is not a vertex. Thus the graph in (c) can be redrawn as follows:

51 51 Connectedness Some useful facts relating circuits and connectedness are collected in the following lemma.

52 52 Connectedness The graphs in (b) and (c) are both made up of three pieces, each of which is itself a connected graph. A connected component of a graph is a connected subgraph of largest possible size.

53 53 Connectedness The fact is that any graph is a kind of union of its connected components.

54 54 Example 4 – Connected Components Find all connected components of the following graph G. Solution: G has three connected components: H 1, H 2, and H 3 with vertex sets V 1, V 2, and V 3 and edge sets E 1, E 2, and E 3, where

55 55 Euler Circuits

56 56 Euler Circuits Now we return to consider general problems similar to the puzzle of the Königsberg bridges. The following definition is made in honor of Euler.

57 57 Euler Circuits The analysis used earlier to solve the puzzle of the Königsberg bridges generalizes to prove the following theorem:

58 58 Euler Circuits We know that the contrapositive of a statement is logically equivalent to the statement. The contrapositive of Theorem 10.2.2 is as follows: This version of Theorem 10.2.2 is useful for showing that a given graph does not have an Euler circuit.

59 59 Example 5 – Showing That a Graph Does Not Have an Euler Circuit Show that the graph below does not have an Euler circuit. Solution: Vertices v 1 and v 3 both have degree 3, which is odd. Hence by (the contrapositive form of) Theorem 10.2.2, this graph does not have an Euler circuit.

60 60 Euler Circuits Now consider the converse of Theorem 10.2.2: If every vertex of a graph has even degree, then the graph has an Euler circuit. Is this true? The answer is no. There is a graph G such that every vertex of G has even degree but G does not have an Euler circuit. In fact, there are many such graphs. The illustration below shows one example.

61 61 Euler Circuits Note that the graph in the preceding drawing is not connected. It turns out that although the converse of Theorem 10.2.2 is false, a modified converse is true: If every vertex of a graph has positive even degree and if the graph is connected, then the graph has an Euler circuit.

62 62 Euler Circuits The proof of this fact is constructive: It contains an algorithm to find an Euler circuit for any connected graph in which every vertex has even degree.

63 63 Euler Circuits A corollary to Theorem 10.2.4 gives a criterion for determining when it is possible to find a walk from one vertex of a graph to another, passing through every vertex of the graph at least once and every edge of the graph exactly once.

64 64 Example 7 – Finding an Euler Trail The floor plan shown below is for a house that is open for public viewing. Is it possible to find a trail that starts in room A, ends in room B, and passes through every interior doorway of the house exactly once? If so, find such a trail.

65 65 Example 7 – Solution Let the floor plan of the house be represented by the graph below. Each vertex of this graph has even degree except for A and B, each of which has degree 1. Hence by Corollary 10.2.5, there is an Euler path from A to B. One such trail is AGHFEIHEKJDCB.

66 66 Hamiltonian Circuits

67 67 Hamiltonian Circuits Theorem: completely answers the following question: Given a graph G, is it possible to find a circuit for G in which all the edges of G appear exactly once? A related question is this: Given a graph G, is it possible to find a circuit for G in which all the vertices of G (except the first and the last) appear exactly once?

68 68 Hamiltonian Circuits In 1859 the Irish mathematician Sir William Rowan Hamilton introduced a puzzle in the shape of a dodecahedron (DOH-dek-a-HEE-dron). (Figure 10.2.6 contains a drawing of a dodecahedron, which is a solid figure with 12 identical pentagonal faces.) Figure 10.2.6 Dodecahedron

69 69 Hamiltonian Circuits Each vertex was labeled with the name of a city—London, Paris, Hong Kong, New York, and so on. The problem Hamilton posed was to start at one city and tour the world by visiting each other city exactly once and returning to the starting city. One way to solve the puzzle is to imagine the surface of the dodecahedron stretched out and laid flat in the plane, as follows:

70 70 Hamiltonian Circuits The circuit denoted with black lines is one solution. Note that although every city is visited, many edges are omitted from the circuit. (More difficult versions of the puzzle required that certain cities be visited in a certain order.) The following definition is made in honor of Hamilton.

71 71 Hamiltonian Circuits Note that although an Euler circuit for a graph G must include every vertex of G, it may visit some vertices more than once and hence may not be a Hamiltonian circuit. On the other hand, a Hamiltonian circuit for G does not need to include all the edges of G and hence may not be an Euler circuit. Despite the analogous-sounding definitions of Euler and Hamiltonian circuits, the mathematics of the two are very different.

72 72 Hamiltonian Circuits Theorem 10.2.4 gives a simple criterion for determining whether a given graph has an Euler circuit. Unfortunately, there is no analogous criterion for determining whether a given graph has a Hamiltonian circuit, nor is there even an efficient algorithm for finding such a circuit.

73 73 Hamiltonian Circuits There is, however, a simple technique that can be used in many cases to show that a graph does not have a Hamiltonian circuit. This follows from the following considerations: Suppose a graph G with at least two vertices has a Hamiltonian circuit C given concretely as Since C is a simple circuit, all the e i are distinct and all the v j are distinct except that v 0 = v n. Let H be the subgraph of G that is formed using the vertices and edges of C.

74 74 Hamiltonian Circuits An example of such an H is shown below. Note that H has the same number of edges as it has vertices since all its n edges are distinct and so are its n vertices v 1, v 2,..., v n. Also, by definition of Hamiltonian circuit, every vertex of G is a vertex of H, and H is connected since any two of its vertices lie on a circuit. In addition, every vertex of H has degree 2.

75 75 Hamiltonian Circuits The reason for this is that there are exactly two edges incident on any vertex. These are e i and e i+1 for any vertex v i except v 0 = v n, and they are e 1 and e n for v 0 (= v n ). These observations have established the truth of the following proposition in all cases where G has at least two vertices.

76 76 Hamiltonian Circuits Note that if G contains only one vertex and G has a Hamiltonian circuit, then the circuit has the form v e v, where v is the vertex of G and e is an edge incident on v. In this case, the subgraph H consisting of v and e satisfies conditions (1)–(4) of Proposition 10.2.6.

77 77 Hamiltonian Circuits We know that the contrapositive of a statement is logically equivalent to the statement. The contrapositive of Proposition 10.2.6 says that if a graph G does not have a subgraph H with properties (1)–(4), then G does not have a Hamiltonian circuit. The next example illustrates a type of problem known as a traveling salesman problem. It is a variation of the problem of finding a Hamiltonian circuit for a graph.

78 78 Example 9 – A Traveling Salesman Problem Imagine that the drawing below is a map showing four cities and the distances in kilometers between them. Suppose that a salesman must travel to each city exactly once, starting and ending in city A. Which route from city to city will minimize the total distance that must be traveled?

79 79 Example 9 – Solution This problem can be solved by writing all possible Hamiltonian circuits starting and ending at A and calculating the total distance traveled for each. Thus either route ABCDA or ADCBA gives a minimum total distance of 125 kilometers.

80 80 Hamiltonian Circuits The general traveling salesman problem involves finding a Hamiltonian circuit to minimize the total distance traveled for an arbitrary graph with n vertices in which each edge is marked with a distance. One way to solve the general problem is to use the method of Example 9: Write down all Hamiltonian circuits starting and ending at a particular vertex, compute the total distance for each, and pick one for which this total is minimal.

81 81 Hamiltonian Circuits However, even for medium-sized values of n this method is impractical. For a complete graph with 30 vertices, there would be Hamiltonian circuits starting and ending at a particular vertex to check. Even if each circuit could be found and its total distance computed in just one nanosecond, it would require approximately 1.4  10 14 years to finish the computation.

82 82 Hamiltonian Circuits At present, there is no known algorithm for solving the general traveling salesman problem that is more efficient. However, there are efficient algorithms that find “pretty good” solutions—that is, circuits that, while not necessarily having the least possible total distances, have smaller total distances than most other Hamiltonian circuits.

83 83 Representation of Graphs

84 Fundamentals of Python: From First Programs Through Data Structures84 Representations of Graphs To represent graphs, you need a convenient way to store the vertices and the edges that connect them Two commonly used representations of graphs: –The adjacency matrix –The adjacency list

85 Fundamentals of Python: From First Programs Through Data Structures85 Adjacency Matrix (continued) Observe the symmetry along the main diagonal: If the vertices are labeled, then the labels can be stored in a separate one-dimensional array 1

86 Fundamentals of Python: From First Programs Through Data Structures86 Adjacency List If a graph has N vertices labeled 0, 1,..., N – 1, –The adjacency list for the graph is an array of N linked lists –The ith linked list contains a node for vertex j if and only if there is an edge from vertex i to vertex j

87 Fundamentals of Python: From First Programs Through Data Structures87 Adjacency List (continued)

88 Fundamentals of Python: From First Programs Through Data Structures88 Adjacency List (continued) A1 What is the bug in this adjacency list?

89 Fundamentals of Python: From First Programs Through Data Structures89 Analysis of the Two Representations Two commonly used graph operations: –Determine whether or not there is an edge between two given vertices The adjacency matrix supports the first operation in constant time The linked adjacency has linear running time with the length of this list, on the average –Find all of the vertices adjacent to a given vertex Adjacency list tends to support this operation more efficiently than the adjacency matrix Both are O(N) in worst case (i.e., a complete graph)

90 Fundamentals of Python: From First Programs Through Data Structures90 Analysis of the Two Representations (continued) For the case of insertions of edges into the lists: –Array-based adjacency list insertion takes linear time –Linked-based adjacency list insertion requires constant time Memory usage: –Adjacency matrix always requires N 2 cells –Adjacency list requires an array of N pointers and a number of nodes equal to twice the number of edges in the case of an undirected graph

91 Fundamentals of Python: From First Programs Through Data Structures91 Further Run-Time Considerations To iterate across all the neighbors of a given vertex (N = number of vertices, M = number of edges): –Adjacency matrix  must traverse a row in a time that is O(N); to repeat this for all rows is O(N 2 ) –Adjacency list  time to traverse across all neighbors depends on the number of neighbors On the average, O(M/N); to repeat this for all vertices is O(max(M, N)) –For a dense graph: O(N 2 ) –For a sparse graph: O(N)

92 Fundamentals of Python: From First Programs Through Data Structures92 Graph Traversals In addition to the insertion and removal of items, important graph-processing operations include: –Finding the shortest path to a given item in a graph –Finding all of the items to which a given item is connected by paths –Traversing all of the items in a graph One starts at a given vertex and, from there, visits all vertices to which it connects Different from tree traversals, which always visit all of the nodes in a given tree

93 Fundamentals of Python: From First Programs Through Data Structures93 A Generic Traversal Algorithm traverseFromVertex(graph, startVertex): mark all vertices in the graph as unvisited insert the startVertex into an empty collection while the collection is not empty: remove a vertex from the collection if the vertex has not been visited: mark the vertex as visited process the vertex insert all adjacent unvisited vertices into the collection

94 Fundamentals of Python: From First Programs Through Data Structures94 Breadth-First and Depth-First Traversals A depth-first traversal, uses a stack as the collection in the generic algorithm A breadth-first traversal, uses a queue as the collection in the generic algorithm

95 Fundamentals of Python: From First Programs Through Data Structures95 Breadth-First and Depth-First Traversals (continued) Recursive depth-first traversal: traverseFromVertex(graph, startVertex): mark all vertices in the graph as unvisited dfs(graph, startVertex) dfs(graph, v): mark v as visited process v for each vertex, w, adjacent to v: if w has not been visited: dfs(graph, w)

96 Fundamentals of Python: From First Programs Through Data Structures96 Breadth-First and Depth-First Traversals (continued) To traverse all vertices (iterative version): traverseAll(graph): mark all vertices in the graph as unvisited instantiate an empty collection for each vertex in the graph: if the vertex has not been visited: insert the vertex in the collection while the collection is not empty: remove a vertex from the collection if the vertex has not been visited: mark the vertex as visited process the vertex insert adjacent unvisited vertices into collection

97 Fundamentals of Python: From First Programs Through Data Structures97 Breadth-First and Depth-First Traversals (continued) To traverse all vertices (recursive version): traverseAll(graph): mark all vertices in the graph as unvisited for each vertex, v, in the graph: if v is unvisited: dfs(graph, v) dfs(graph g, v): mark v as visited process v for each vertex, w, adjacent to v: if w is unvisited dfs(g, w)

98 Fundamentals of Python: From First Programs Through Data Structures98 Graph Components partitionIntoComponents(graph): lyst = [] mark all vertices in the graph as unvisited for each vertex, v, in the graph: if v is unvisited: s = set() lyst.append(s) dfs (g, v, s) return list dfs(graph, v, s): mark v as visited s.add(v) for each vertex, w, adjacent to v: if w is unvisited: dfs(g, w, s)

99 Fundamentals of Python: From First Programs Through Data Structures99 Trees Within Graphs traverseFromVertex yields a tree rooted at the vertex from which the traversal starts and includes all the vertices reached during the traversal –Tree is just a subgraph of the graph being traversed –If dfs has been used, we build a depth-first search tree –It is also possible to build a breadth-first search tree

100 Fundamentals of Python: From First Programs Through Data Structures100 Spanning Trees and Forests A spanning tree has the fewest number of edges possible while still retaining a connection between all the vertices in the component –If the component contains n vertices, the spanning tree contains n – 1 edges When you traverse all the vertices of an undirected graph, you generate a spanning forest

101 Fundamentals of Python: From First Programs Through Data Structures101 Minimum Spanning Tree In a weighted graph, you can sum the weights for all edges in a spanning tree and attempt to find a spanning tree that minimizes this sum –There are several algorithms for finding a minimum spanning tree for a component. Repeated application to all the components in a graph yields a minimum spanning forest For example, to determine how an airline can service all cities, while minimizing the total length of the routes it needs to support

102 Fundamentals of Python: From First Programs Through Data Structures102 Algorithms for Minimum Spanning Trees There are two well-known algorithms for finding a minimum spanning tree: –One developed by Robert C. Prim in 1957 –The other by Joseph Kruskal in 1956 Here is Prim’s algorithm: minimumSpanningTree(graph): mark all vertices and edges as unvisited mark some vertex, say v, as visited for all the vertices: find the least weight edge from a visited vertex to an unvisited vertex, say w mark the edge and w as visited –Maximum running time is O(m * n)

103 Fundamentals of Python: From First Programs Through Data Structures103 Algorithms for Minimum Spanning Trees (continued) Improvement to algorithm: use a heap of edges –Maximum running time: O(m log n) for adjacency list

104 Fundamentals of Python: From First Programs Through Data Structures104 Topological Sort A directed acyclic graph (DAG) has an order among the vertices A topological order assigns a rank to each vertex such that the edges go from lower- to higher- ranked vertices Topological sort: process of finding and returning a topological order of vertices in a graph One topological sort algorithm is based on a graph traversal –Performance is O(m) when stack insertions are O(1)

105 Fundamentals of Python: From First Programs Through Data Structures105 Topological Sort (continued)

106 Fundamentals of Python: From First Programs Through Data Structures106 Topological Sort (continued) topologicalSort(graph g): stack = LinkedStack() mark all vertices in the graph as unvisited for each vertex, v, in the graph: if v is unvisited: dfs(g, v, stack) return stack dfs(graph, v, stack): mark v as visited for each vertex, w, adjacent to v: if w is unvisited: dfs(graph, w, stack) stack.push(v)

107 Fundamentals of Python: From First Programs Through Data Structures107 The Shortest-Path Problem The single-source shortest path problem asks for a solution that contains the shortest paths from a given vertex to all of the other vertices –Has a widely used solution by Dijkstra Is O(n 2 ) and assumes that all weights must be positive The all-pairs shortest path problem, asks for the set of all the shortest paths in a graph –A widely used solution by Floyd is O(n 3 )

108 Fundamentals of Python: From First Programs Through Data Structures108 Dijkstra’s Algorithm Inputs: a directed acyclic graph with edge weights > 0 and the source vertex Computes the distances of the shortest paths from source vertex to all other vertices in graph Output: a two-dimensional grid, results –N rows, where N is the number of vertices Uses a temporary list, included, of N Booleans to track if shortest path has already been determined for a vertex Steps: initialization step and computation step

109 Fundamentals of Python: From First Programs Through Data Structures109 The Initialization Step for each vertex in the graph Store vertex in the current row of the results grid If vertex = source vertex Set the row’s distance cell to 0 Set the row’s parent cell to undefined Set included[row] to True Else if there is an edge from source vertex to vertex Set the row’s distance cell to the edge’s weight Set the row’s parent cell to source vertex Set included[row] to False Else Set the row’s distance cell to infinity Set the row’s parent cell to undefined Set included[row] to False Go to the next row in the results grid

110 Fundamentals of Python: From First Programs Through Data Structures110 The Initialization Step (continued)

111 Fundamentals of Python: From First Programs Through Data Structures111 The Computation Step Do Find the vertex F that is not yet included and has the minimal distance in the results grid Mark F as included For each other vertex T not included If there is an edge from F to T Set new distance to F’s distance + edge’s weight If new distance < T’s distance in the results grid Set T’s distance to new distance Set T’s parent in the results grid to F While at least one vertex is not included

112 Fundamentals of Python: From First Programs Through Data Structures112 The Computation Step (continued)

113 Fundamentals of Python: From First Programs Through Data Structures113 Analysis The initialization step must process every vertex, so it is O(n) The outer loop of the computation step also iterates through every vertex –The inner loop of this step iterates through every vertex not included thus far –Hence, the overall behavior of the computation step resembles that of other O(n 2 ) algorithms Dijkstra’s algorithm is O(n 2 )

114 Fundamentals of Python: From First Programs Through Data Structures114 Developing a Graph ADT The graph ADT shown here creates weighted directed graphs with an adjacency list representation In the examples, the vertices are labeled with strings and the edges are weighted with numbers The implementation of the graph ADT shown here consists of the classes: –LinkedDirectedGraph –LinkedVertex –LinkedEdge

115 Fundamentals of Python: From First Programs Through Data Structures115 Example Use of the Graph ADT Assume that you want to create the following weighted directed graph:

116 Fundamentals of Python: From First Programs Through Data Structures116 Example Use of the Graph ADT (continued)

117 Fundamentals of Python: From First Programs Through Data Structures117 Example Use of the Graph ADT (continued) To display the neighboring vertices and the incident edges of the vertex labeled A : Output:

118 Fundamentals of Python: From First Programs Through Data Structures118 The Class LinkedDirectedGraph

119 Fundamentals of Python: From First Programs Through Data Structures119 The Class LinkedVertex

120 Fundamentals of Python: From First Programs Through Data Structures120 The Class LinkedEdge

121 Fundamentals of Python: From First Programs Through Data Structures121 The Class LinkedEdge (continued)

122 Fundamentals of Python: From First Programs Through Data Structures122 Case Study: Testing Graph Algorithms Request: –Write a program that allows the user to test some graph-processing algorithms Analysis: –The program consists of two main classes, GraphDemoView and GraphDemoModel

123 Fundamentals of Python: From First Programs Through Data Structures123 Case Study: Testing Graph Algorithms (continued) The Classes GraphDemoView and GraphDemoModel :

124 Fundamentals of Python: From First Programs Through Data Structures124 Case Study: Testing Graph Algorithms (continued)

125 Fundamentals of Python: From First Programs Through Data Structures125 Case Study: Testing Graph Algorithms (continued) Implementation (Coding): –The view class includes methods for displaying the menu and getting a command that are similar to methods in other case studies The other two methods get the inputs from the keyboard or a file –The model class includes methods to create a graph and run a graph-processing algorithm –The functions defined in the algorithms module accept two arguments: a graph and a start label When the start label is not used, it can be defined as an optional argument

126 Fundamentals of Python: From First Programs Through Data Structures126 Summary Graphs have many applications A graph consists of one or more vertices (items) connected by one or more edges Path: Sequence of edges that allows one vertex to be reached from another vertex in the graph A graph is connected if there is a path from each vertex to every other vertex A graph is complete if there is an edge from each vertex to every other vertex

127 Fundamentals of Python: From First Programs Through Data Structures127 Summary (continued) A subgraph consists of a subset of a graph’s vertices and a subset of its edges Connected component: a subgraph consisting of the set of vertices reachable from a given vertex Directed graphs allow travel along an edge in just one direction Edges can be labeled with weights, which indicate the cost of traveling along them Graphs have two common implementations: –Adjacency matrix and adjacency list

128 Fundamentals of Python: From First Programs Through Data Structures128 Summary (continued) Graph traversals explore tree-like structures within a graph, starting with a distinguished start vertex –e.g., depth-first traversal and breadth-first traversal A minimum spanning tree is a spanning tree whose edges contain the minimum weights possible A topological sort generates a sequence of vertices in a directed acyclic graph The single-source shortest path problem asks for a solution that contains the shortest paths from a given vertex to all of the other vertices

129 129 ______________________ Devon M. Simmonds Computer Science Department University of North Carolina Wilmington _____________________________________________________________ Qu es ti ons? Reading from course text:

130 Fundamentals of Python: From First Programs Through Data Structures130 Adjacency Matrix (continued) If the graph is undirected, then four more cells are occupied by 1: If the vertices are labeled, then the labels can be stored in a separate one-dimensional array 1


Download ppt "Fundamentals of Python: From First Programs Through Data Structures Chapter 20 Graphs."

Similar presentations


Ads by Google