Download presentation
Presentation is loading. Please wait.
1
Data Structures 13th Week
Chapter 6 Graphs 6.1 The Graph Abstract Data Type 6.2 Elementary Graph Operations 6.3 Minimum Cost Spanning Trees
2
The Graph Abstract Data Type
A graph, G, consists of two sets : a finite, nonempty set of vertices, and a finite, possibly empty set of edges. V(G) and E(G) represent the sets of vertices and edges of G, respectively. Alternately, we may write G = (V, E) to represent a graph. Undirected graph is one in which the pair of vertices representing any edge is unordered. (v0, v1), (v1, v0) represent the same edge. Directed graph is one in which we represent each edge as a directed pair of vertices. <v0, v1> represents an edge in which v0 is the tail and v1 is the head.
3
Sample Graphs G G G3 V(G1) = {0, 1, 2, 3} E(G1) = {(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)} V(G2) = {0, 1, 2, 3, 4, 5, 6} E(G2) = {(0,1), (0,2), (1,3), (1,4), (2,5), (2,6)} V(G3) = {0, 1, 2} E(G3) = {<0,1>, <1,0>, <1,2> }
4
Definitions complete graph adjacent, incident subgraph path
graph that has the maximum number of edges. adjacent, incident if (v0, v1) is an edge in an undirected graph, then the vertices v0 and v1 are adjacent and the edge (v0, v1) is incident on vertices v0 and v1 subgraph a subgraph of G is a graph G’ such that V(G’ ) V(G) and E(G’) E(G) path a path from vertex vp to vertex vq in a graph, G, is a sequence of vertices, vp, vi1, vi2, … , vin , vq such that (vp, vi1), (vi1, vi2), … , (vin , vq) are edges in an undirected graph.
5
the length of a path is the number of edges on it.
simple path : path in which all vertices, except possibly the first and the last, are distinct. cycle : simple path in which the first and the last vertices are the same. connected In an undirected graph G, two vertices, v0 and v1, are connected if there is a path in G from v0 to v1. connected component : maximal connected subgraph tree : graph that is connected and a cyclic (it has no cycles). The degree of a vertex is the number of edges incident to the vertex.
6
restriction on graphs :
1. A graph may not have an edge from a vertex, i, back to itself. self loops. 2. A graph may not have multiple occurrences of the same edge. if we allow, multigraph.
7
Graph Representations
Adjacency Matrix The adjacency matrix of G is a two-dimensional n n array, say adj_mat. If the edge (vi, vj) ( <vi, vj> for a digraph) is in E(G), adj_mat[i][j] = 1. If there is no edge in E(G), adj_mat[i][j] = 0. ex)
8
Adjacency Lists replace the n rows of the adjacency matrix with n linked lists, one for each vertex in G. Ex)
9
Adjacency Multilists maintaining the lists as multilists, that is, lists in which nodes are shared among several lists, facilitates this operation. ex) The lists are : vertex 0 : N1 N2 N3 vertex 1 : N1 N4 N5 vertex 2 : N2 N4 N6 vertex 3 : N3 N5 N6
10
Depth First Search Algorithm
1. begin the search by visiting the start vertex, v. 2. select an unvisited vertex, w, from v’s adjacency list and carry out a depth first search on w. (using recursion)
11
Depth First Search Function
#define FALSE 0 #define TRUE 1 short int visited[MAX_VERTICES]; void dfs(int v) { /* depth first search of a graph beginning with vertex v. */ node_pointer w; visited[v]=TRUE; printf(“5d”,v); for (w = graph[v]; w; w = w->link) if( !visited[w->vertex] ) dfs(w->vertex); }
12
ex) Depth first search order : v0, v1, v3, v7, v4, v5, v2, v6
13
Breadth First Search Algorithm
1. starts at vertex v and marks it as visited. 2. visits each of the vertices on v’s adjacency list. 3. when we have visited all the vertices on v’s adjacency list, we visit all the unvisited vertices that are adjacent to the first vertex on v’s adjacency list. using queue.
14
Connected Components whether or not an undirected graph is connected?
Calling either dfs(0) of bfs(0) and then determining if there are any unvisited vertices. Listing the connected components of a graph. void connected(void) { /* determine the connected components of a graph */ int i; for ( i = 0; i < n; i++ ) if ( !visited[i] ) { dfs(i); printf(“\n”); }
15
Biconnected Components And Articulation Points
Definition An articulation point is a vertex v of G such that the deletion of v, together with all edges incident on v, produces a graph, G’ that has at least two connected components. A biconnected graph is a connected graph that has no articulation points. A biconnected component of a connected undirected graph is a maximal biconnected subgraph, H, of G. The maximal biconnected subgraph means that G contains no other subgraph that is both biconnected and properly contains H.
16
ex) articulation point : 1, 3 ,5 ,7
17
Spanning Trees A spanning tree is any tree that consists solely of edge in G and that include all vertices in G. Use either dfs or bfs to create a spanning tree. depth first spanning tree breath first spanning tree
18
Minimum Cost Spanning Trees
Definition cost of a spanning tree of a weighted undirected graph : the sum of the costs(weights) of the edges in the spanning tree Minimum cost spanning tree : a spanning tree of least cost Three algorithms for minimum cost spanning tree Kruskal’s, Prim’s, Sollin’s algorithms All three use an greedy method Greedy method At each stage, make a decision that is the best decision (using the criterion) Since we cannot change this decision later, we make sure that the decision will result in a feasible solution Typical criterion : least cost, highest profit criterion
19
Minimum Cost Spanning Trees (Cont’d)
Greedy method At each stage, make a decision that is the best decision (using the criterion). Since we cannot change this decision later, we make sure that the decision will result in a feasible solution. Typical criterion : least cost, highest profit criterion Constraints for minimum cost spanning tree algorithms 1. Must use only edges within the graph. 2. Must use exactly n-1 edges. 3. May not use edges that would produce a cycle.
20
Kruskal’s Algorithm Methods
build a minimum cost tree T by adding edges to T one at a time selects the edges for inclusion in T in nondecreasing order of their cost an edge is added in T if it does not form a cycle graph G is connected and has n>0 vertices, exactly n-1 edges will be selected
21
(Ex) Kruskal’s Algorithm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.