11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sec. 11.1 Terminology <strong>and</strong> Represent<strong>at</strong>ions 3730 26741 35Figure 11.2 An undirected graph with three connected components. Vertices 0,1, 2, 3, <strong>and</strong> 4 form one connected component. Vertices 5 <strong>and</strong> 6 form a secondconnected component. Vertex 7 by itself forms a third connected component.A subgraph S is formed from graph G by selecting a subset V s of G’s vertices<strong>and</strong> a subset E s of G’s edges such th<strong>at</strong> for every edge E in E s , both of E’s verticesare in V s .An undirected graph is connected if there is <strong>at</strong> least one p<strong>at</strong>h from any vertexto any other. The maximally connected subgraphs of an undirected graph are calledconnected components. For example, Figure 11.2 shows an undirected graph withthree connected components.A graph without cycles is called acyclic. Thus, a directed graph without cyclesis called a directed acyclic graph or DAG.A free tree is a connected, undirected graph with no simple cycles. An equivalentdefinition is th<strong>at</strong> a free tree is connected <strong>and</strong> has |V| − 1 edges.There are two commonly used methods for representing graphs. The adjacencym<strong>at</strong>rix is illustr<strong>at</strong>ed by Figure 11.3(b). The adjacency m<strong>at</strong>rix for a graphis a |V| × |V| array. Assume th<strong>at</strong> |V| = n <strong>and</strong> th<strong>at</strong> the vertices are labeled fromv 0 through v n−1 . Row i of the adjacency m<strong>at</strong>rix contains entries for Vertex v i .Column j in row i is marked if there is an edge from v i to v j <strong>and</strong> is not marked otherwise.Thus, the adjacency m<strong>at</strong>rix requires one bit <strong>at</strong> each position. Altern<strong>at</strong>ively,if we wish to associ<strong>at</strong>e a number with each edge, such as the weight or distancebetween two vertices, then each m<strong>at</strong>rix position must store th<strong>at</strong> number. In eithercase, the space requirements for the adjacency m<strong>at</strong>rix are Θ(|V| 2 ).The second common represent<strong>at</strong>ion for graphs is the adjacency list, illustr<strong>at</strong>edby Figure 11.3(c). The adjacency list is an array of linked lists. The array is|V| items long, with position i storing a pointer to the linked list of edges for Vertexv i . This linked list represents the edges by the vertices th<strong>at</strong> are adjacent toVertex v i . The adjacency list is therefore a generaliz<strong>at</strong>ion of the “list of children”represent<strong>at</strong>ion for trees described in Section 6.3.1.Example 11.1 The entry for Vertex 0 in Figure 11.3(c) stores 1 <strong>and</strong> 4because there are two edges in the graph leaving Vertex 0, with one going

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!