Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Chapter 11 Objectives Upon completion you will be able to:

Similar presentations


Presentation on theme: "Graphs Chapter 11 Objectives Upon completion you will be able to:"— Presentation transcript:

1 Graphs Chapter 11 Objectives Upon completion you will be able to:
Understand and use basic graph terminology and concepts Define and discuss basic graph and network structures Design and implement graph and network applications Design and implement applications using the graph ADT Define and discuss Dijkstra's shortest path algorithm Data Structures: A Pseudocode Approach with C, Second Edition

2 11-1 Basic Concepts Directed and Undirected Graphs Cycles and Loops
A graph is a collection of nodes, called vertices, and a collection of segments, called lines, connecting pairs of vertices. In Basic Concepts we develop the terminology and structure for graphs. In addition to the graph definitions, discussion points include: Directed and Undirected Graphs Cycles and Loops Connected and Disjoint Graphs Data Structures: A Pseudocode Approach with C, Second Edition

3 Introduction in the last chapter, we turn our attention to a data structure, graphs, that differs from all of the others in one major concept: each node may have multiple predecessors as well as multiple successors graphs are very useful structures they can used to solve complex routing problems such as designing and routine airlines among the airports they serve they can be used to route messages over a computer network from one node to another Data Structures: A Pseudocode Approach with C, Second Edition

4 Terminology a graph consists of 2 sets
a collection of nodes, called vertices and a collection of line segment connecting pairs of vertices, called lines a directed graph, or digraph for short, is a graph in which each line has a direction (arrow head) to its successor the lines in a directed graph are know as arcs Data Structures: A Pseudocode Approach with C, Second Edition

5 Terminology (2) in a directed graph, the flow along the arcs between two vertices can follow only the indicated direction an undirected graph is a graph in which there is no direction on the lines, know as edges in an undirected graph, the flow between two vertices can go in either direction Data Structures: A Pseudocode Approach with C, Second Edition

6 Data Structures: A Pseudocode Approach with C, Second Edition

7 Terminology (3) two vertices in a graph are said to be adjacent vertices (neighbors) if there exists an edge that directly connects them from figure 11-1, A and B are adjacent D and F are not adjacent a path is a sequence of vertices in which each vertex is adjacent to the next one (A, B, C, E ) is one path (A, B, E, F) is another path Data Structures: A Pseudocode Approach with C, Second Edition

8 Terminology (4) A cycle is a path consisting of at least 3 vertices that starts and ends with the same vertex figure 11-1 (b) B, C, D, E, B is a cycle (a) does not constitute a cycle a loop is a special case of a cycle in which a single arc begins and ends with the same vertex in a loop, the end points of the edge are the same Data Structures: A Pseudocode Approach with C, Second Edition

9 Terminology (5) two vertices are said to be connected if there is a path between them a graph is said to be connected if, suppressing direction, there is a path from any vertex any other vertex furthermore, a directed graph is strongly connected if there is a path from each vertex to every other vertex in the digraph Data Structures: A Pseudocode Approach with C, Second Edition

10 Terminology (6) a directed graph is weakly connected, when there are at least two vertices that are not connected a graph is disjoint if it is not connected figure 11-2 contains a weakly connected graph (a) a strong connected graph (b) and disjoint graph (c) Data Structures: A Pseudocode Approach with C, Second Edition

11 Data Structures: A Pseudocode Approach with C, Second Edition

12 Data Structures: A Pseudocode Approach with C, Second Edition

13 Terminology (7) the degree of a vertex is the number of lines incident to it figure 11-2(a), the degree of vertex B is 3 and the degree of vertex E is 4 the outdegree of a vertex in a digraph is the number of arcs leaving the vertex the indegree is the number of arcs entering the vertex Data Structures: A Pseudocode Approach with C, Second Edition

14 11-2 Operations Insert Vertex Delete Vertex Add Edge Delete Edge
We define and discuss the six primitive graph operations required to maintain a graph. Insert Vertex Delete Vertex Add Edge Delete Edge Find Vertex Traverse Graph Data Structures: A Pseudocode Approach with C, Second Edition

15 Add vertex given a graph, add vertex inserts a new vertex into the graph when a vertex is added, it is disjoint (it is not connected to any other vertices in the list) after a vertex is added, it must be connected Data Structures: A Pseudocode Approach with C, Second Edition

16 Data Structures: A Pseudocode Approach with C, Second Edition

17 Delete vertex delete vertex removes a vertex from the graph
when a vertex is deleted all connecting edges are also removed Data Structures: A Pseudocode Approach with C, Second Edition

18 Data Structures: A Pseudocode Approach with C, Second Edition

19 Add edge add an edge connects a vertex to a destination vertex
if a vertex requires multiple edges, then add an edge must be called once for each adjacent vertex to ad an edge, two vertices must be specified the graph is a digraph, then one of the vertices must be specified as the source and one as the destination Data Structures: A Pseudocode Approach with C, Second Edition

20 Data Structures: A Pseudocode Approach with C, Second Edition

21 Delete edge delete an edge removes one edge from a graph
figure 11-6 contains an example, deleting the edge {A, E} from the graph Data Structures: A Pseudocode Approach with C, Second Edition

22 Data Structures: A Pseudocode Approach with C, Second Edition

23 Find Vertex find vertex traverses a graph looking for a specified vertex if the vertex is found, its address is returned if it is not found, an error is returned Data Structures: A Pseudocode Approach with C, Second Edition

24 Data Structures: A Pseudocode Approach with C, Second Edition

25 Traverse Graph given a graph, there is always at least one application that requires that all vertices in the graph be visited that is there is at least one application that requires that the graph be traversed because a vertex in a graph can have multiple parents, the traversal of a graph presents some problems not found in the traversal of linear lists and trees Data Structures: A Pseudocode Approach with C, Second Edition

26 Traverse Graph (2) specifically, we must somehow assure that we process the data in each vertex only once because there are multiple paths to a vertex, it is possible that we can arrive at it from more than one direction as we traverse the graph the traditional solution to this problem is to include a visited flag at each vertex before the traversal, the graph is scanned and the visited flag is set off Data Structures: A Pseudocode Approach with C, Second Edition

27 Traverse Graph (3) then, as we traverse the graph, we set the visited flag on to indicate the at the data have already been processed there are 2 standard graph traversal depth–first traversal breadth-first traversal Data Structures: A Pseudocode Approach with C, Second Edition

28 Depth-first traversal
in the depth-first traversal, all of a vertex’s descendents are processed before we move to an adjacent vertex the depth-first traversal of a graph starts by processing the first vertex of the graph after processing the first vertex, we select any vertex adjacent to the first vertex and process it as each vertex is processed, we select an adjacent vertex until we reach a vertex with no adjacent entries Data Structures: A Pseudocode Approach with C, Second Edition

29 Depth-first traversal (2)
this is similar to reaching a leaf in a tree we then back out of the structure, processing adjacent vertices as we go it should be obvious that this logic requires a stack (or recursion) to complete the traversal the order in which the adjacent vertices are processed depends on how the graph is physically stored Data Structures: A Pseudocode Approach with C, Second Edition

30 Data Structures: A Pseudocode Approach with C, Second Edition

31 Data Structures: A Pseudocode Approach with C, Second Edition

32 Breadth-first in the breadth-first traversal of a graph, all adjacent vertices of a vertex are processed before going to the next level the breadth-first traversal of a graph begins by picking a starting vertex and after processing it, we process all of its adjacent vertices when all of the adjacent vertices have been processed, we pick the first adjacent vertex and process all of its vertices, then the second adjacent vertex and process all of its vertices, and so forth until we done Data Structures: A Pseudocode Approach with C, Second Edition

33 Data Structures: A Pseudocode Approach with C, Second Edition

34 Data Structures: A Pseudocode Approach with C, Second Edition

35 11-3 Graph Storage Structures
To represent a graph, we need to store two sets. The first set represents the vertices of the graph, and the second set represents the edges or arcs. The two most common structures used to store these sets are arrays and linked lists. Adjacency Matrix Adjacency List Data Structures: A Pseudocode Approach with C, Second Edition

36 Adjacency matrix the adjacency matrix uses a vector (one-dimensional array) for the vertices and a matrix (2-dimensional array) to store the edges if 2 vertices are adjacent, that is if there is an edge between them, the matrix intersect has a value of 1 if there is no edge between them, the intersect is set to 0 Data Structures: A Pseudocode Approach with C, Second Edition

37 Data Structures: A Pseudocode Approach with C, Second Edition

38 Adjacency matrix (2) in addition to the limitation that the size of the graph must be know before the program starts, there is another serious limitation in the adjacency matrix only one edge can be stored between any 2 vertices while this does not prevent many graphs from using the matrix format, some network structures do require multiple edges between vertices Data Structures: A Pseudocode Approach with C, Second Edition

39 Adjacency list the adjacency list uses a 2-dimensional ragged array to store the edges the vertex list is a singly-linked list of the vertices in the list depending on the application, it could also be implemented using doubly-linked lists or circularly-linked list the pointer at the left of the list links the vertex entries together Data Structures: A Pseudocode Approach with C, Second Edition

40 Data Structures: A Pseudocode Approach with C, Second Edition

41 11-4 Graph Algorithms Create Graph Insert Vertex Delete Vertex
In this section we develop a minimum set of algorithms that are needed to create and maintain a directed graph. Create Graph Insert Vertex Delete Vertex Insert Arc Delete Arc Retrieve Vertex Depth-first Traversal Breadth-first Traversal Destroy Graph Data Structures: A Pseudocode Approach with C, Second Edition

42 Data Structures: A Pseudocode Approach with C, Second Edition

43 Data Structures: A Pseudocode Approach with C, Second Edition

44 Data Structures: A Pseudocode Approach with C, Second Edition

45 Data Structures: A Pseudocode Approach with C, Second Edition

46 Data Structures: A Pseudocode Approach with C, Second Edition

47 Data Structures: A Pseudocode Approach with C, Second Edition

48 (continued) Data Structures: A Pseudocode Approach with C, Second Edition

49 Data Structures: A Pseudocode Approach with C, Second Edition

50 (continued) Data Structures: A Pseudocode Approach with C, Second Edition

51 Data Structures: A Pseudocode Approach with C, Second Edition

52 Data Structures: A Pseudocode Approach with C, Second Edition

53 (continued) Data Structures: A Pseudocode Approach with C, Second Edition

54 Data Structures: A Pseudocode Approach with C, Second Edition

55 Data Structures: A Pseudocode Approach with C, Second Edition

56 11-5 Graph ADT Data Structure Functions
We begin by developing the code for the three graph structures. We then develop the code for the ADT functions. Data Structure Functions Data Structures: A Pseudocode Approach with C, Second Edition

57 Data Structures: A Pseudocode Approach with C, Second Edition

58 Data Structures: A Pseudocode Approach with C, Second Edition

59 Data Structures: A Pseudocode Approach with C, Second Edition

60 (continued) Data Structures: A Pseudocode Approach with C, Second Edition

61 (continued) Data Structures: A Pseudocode Approach with C, Second Edition

62 Data Structures: A Pseudocode Approach with C, Second Edition

63 Data Structures: A Pseudocode Approach with C, Second Edition

64 Data Structures: A Pseudocode Approach with C, Second Edition

65 Data Structures: A Pseudocode Approach with C, Second Edition

66 Data Structures: A Pseudocode Approach with C, Second Edition

67 (continued) Data Structures: A Pseudocode Approach with C, Second Edition

68 Data Structures: A Pseudocode Approach with C, Second Edition

69 Data Structures: A Pseudocode Approach with C, Second Edition

70 (continued) Data Structures: A Pseudocode Approach with C, Second Edition

71 Data Structures: A Pseudocode Approach with C, Second Edition

72 Data Structures: A Pseudocode Approach with C, Second Edition

73 Data Structures: A Pseudocode Approach with C, Second Edition

74 Data Structures: A Pseudocode Approach with C, Second Edition

75 (continued) Data Structures: A Pseudocode Approach with C, Second Edition

76 Data Structures: A Pseudocode Approach with C, Second Edition

77 Data Structures: A Pseudocode Approach with C, Second Edition

78 Data Structures: A Pseudocode Approach with C, Second Edition

79 Data Structures: A Pseudocode Approach with C, Second Edition

80 Data Structures: A Pseudocode Approach with C, Second Edition

81 Data Structures: A Pseudocode Approach with C, Second Edition

82 Data Structures: A Pseudocode Approach with C, Second Edition

83 Data Structures: A Pseudocode Approach with C, Second Edition

84 Data Structures: A Pseudocode Approach with C, Second Edition

85 11-6 Networks Minimum Spanning Tree Shortest Path Algorithm
A network is a graph whose lines are weighted. It is also known as a weighted graph. Included in this section are two graph applications that process networks. Minimum Spanning Tree Shortest Path Algorithm Data Structures: A Pseudocode Approach with C, Second Edition

86 Networks (2) example: an airline might use a graph to represent the routes between cities that it serves the cities would be represented by the vertices and the edge a route between two cities the edge’s weight could represent the flight miles between the two cities or it cold represent the price of the flight Data Structures: A Pseudocode Approach with C, Second Edition

87 Data Structures: A Pseudocode Approach with C, Second Edition

88 Networks (3) because the weight is an attribute of an edge, it is stored in the structure that contains the edge in an adjacency matrix, the weight would be stored as the intersection value in an adjacency list, it would be stored as the value in the adjacency linked list Data Structures: A Pseudocode Approach with C, Second Edition

89 Data Structures: A Pseudocode Approach with C, Second Edition

90 Networks (4) there are 2 application of networks
the minimum spanning tree and the shortest path through a network Data Structures: A Pseudocode Approach with C, Second Edition

91 Data Structures: A Pseudocode Approach with C, Second Edition

92 Minimum spanning tree given a connected network, we derived one or more spanning trees from the network a spanning tree is a tree that contains all of the vertices in the graph one of the more interesting algs derives the minimum spanning tree of a network such that the sum of its weights are guaranteed to be minimal Data Structures: A Pseudocode Approach with C, Second Edition

93 Minimum spanning tree (2)
if the weights in the network are unique then there will be only one minimum spanning tree if there are duplicate weights then there may be one or more minimum spanning trees there are many applications for minimum spanning tree, all with the requirement to minimize some aspect of the graph such as the distance among all of the vertices in the graph Data Structures: A Pseudocode Approach with C, Second Edition

94 Minimum spanning tree (3)
example: given a network of computers, we could create a tree that connects all of the computers the minimum spanning tree gives us the shortest length of cable that can be used to connect all the computers together while ensuring that there is a path between any 2 computers Data Structures: A Pseudocode Approach with C, Second Edition

95 Minimum spanning tree (3)
to create a minimum spanning tree in a strongly connected network, that is in a network in which there is a path between any 2 vertices, the edges for the minimum spanning tree are chosen so that the following properties exist: every vertex is included the total edge weight of the spanning tree is the minimum possible that includes a path between any 2 vertices Data Structures: A Pseudocode Approach with C, Second Edition

96 Example from figure 11-20, we can start with any vertex
let’s start with A Data Structures: A Pseudocode Approach with C, Second Edition

97 Data Structures: A Pseudocode Approach with C, Second Edition

98 Data Structures: A Pseudocode Approach with C, Second Edition

99 Data Structures: A Pseudocode Approach with C, Second Edition

100 Data Structures: A Pseudocode Approach with C, Second Edition

101 Data Structures: A Pseudocode Approach with C, Second Edition

102 Shortest path algorithm
an application used with graph requires that we find the shortest path between 2 vertices in a network example: if the network represents the routes flown by an airline, when we travel we would like find the least expensive route between home and our destination the dijkstra alg is used to find the shortest path between any 2 nodes in a graph Data Structures: A Pseudocode Approach with C, Second Edition

103 Data Structures: A Pseudocode Approach with C, Second Edition

104 (continued) Data Structures: A Pseudocode Approach with C, Second Edition

105 Data Structures: A Pseudocode Approach with C, Second Edition

106 Data Structures: A Pseudocode Approach with C, Second Edition

107 Data Structures: A Pseudocode Approach with C, Second Edition

108 Data Structures: A Pseudocode Approach with C, Second Edition


Download ppt "Graphs Chapter 11 Objectives Upon completion you will be able to:"

Similar presentations


Ads by Google