Tải bản đầy đủ (.pdf) (6 trang)

INTRODUCTION TO COMPUTER SCIENCE - PART 6 doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (128.08 KB, 6 trang )

INTRODUCTION TO COMPUTER SCIENCE
HANDOUT #6. THE GRAPH DATA MODEL
K5 & K6, Computer Science Department, Vaên Lang University
Second semester Feb, 2002
Instructor: Traàn Ñöùc Quang
Major themes:
1. Basic Concepts
2. Implementation of Graphs
3. Connected Components of an Undirected Graph
Reading: Sections 9.2, 9.3, and 9.4.
6.1 BASIC CONCEPTS
The graph is a generalization of the tree that was studied in the previous week. Rather
than parent-child relationships, an edge in a graph may represents any binary rela-
tionship between two objects, each represented by a node.
Sometimes we need to indicate explicitly the direction of a relationship by using
arrows rather than edges. In this case, the graph is directed and edges are called arcs.
Formally, we can define a directed graph as a set N of nodes, and a set A of arcs
representing a binary relation on N. Graphs can be drawn as suggested in the figure.
2
3
1
5
7
6
4
0
34 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #6. THE GRAPH DATA MODEL
1. An arrow from node a to b is written (a, b) or a → b. We call a the head of the
arc and b the tail. We also say that a is a predecessor of b, and conversely, b is a
successor of a. In the above figure, the arc 1 → 1 tells us that node 1 is both a
predecessor and a successor of itself. The arc 1 → 1 is also called a loop.


2. A path in a directed graph is a list of nodes (n
1
, n
2
, . . . , n
k
) such that there is an
arc from each node to the next, that is, n
i
→ n
i+1
for i = 1, 2, . . . , k − 1. The
length of the path is k − 1, the number of arcs along the path. In the figure, there
are two paths from node 1 to node 4, one is (1, 2, 3, 4) with length 3, the other
is (1, 3, 4) with length 2.
3. A cycle in a directed graph is a path of length 1 or more that begins and ends at
the same node. In the figure, the path (4, 5, 7, 4) is a cycle of length 3; the path
(1, 1) is a cycle of length 1. If a graph has one or more cycle, we say the graph is
cyclic; otherwise, it is acyclic.
In an undirected graph, an edge between two node a and b is denoted by {a, b}.
Those nodes are neighbors, not a predecessor and a successor.
6.2 IMPLEMENTATION OF GRAPHS
There are two standard ways to represent a graph: adjacency lists and adjacency
matrices. We shall consider these representation for directed graphs.
Adjacency Lists
For simplicity, let nodes be named by the integers 0, 1, . . . , MAX − 1. We also use
NODE as the type of nodes, but we may suppose that NODE is a synonym for int. A
structure for a node can be defined as:
typedef struct CELL *LIST;
struct CELL {

NODE nodeName;
LIST next;
};
The successors of a node form a linked list of cells. To hold the headers of linked lists,
we create an array successors[MAX] of type LIST.
LIST successors[MAX];
That is, the entry successors[u] contains a pointer to a linked list of all the succes-
sors of node u.
The adjacency lists for the graph of the previous figure are suggested in the figure
on the next page.
6.2 IMPLEMENTATION OF GRAPHS 35
Adjacency Matrices
An adjacency matrix is a two-dimensional array arcs[MAX][MAX] of type BOOLEAN. If
there is an arc u → v, the value of the entry arcs[u][v] is TRUE; otherwise, it is
FALSE. Note that we use BOOLEAN as a synonym for int.
typedef int BOOLEAN;
BOOLEAN arcs[MAX][MAX];
The adjacency matrix for our graph is shown below. We use 1 for TRUE and 0 for
FALSE.
For an undirected graph, an edge can be viewed as an arc in both directions, and the
graph can be represented as for directed graphs.
0 1 2 3 4 5 6 7
0
1
2
3
4
5
6
7

1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
1
0
0
0

0
0
0
0
0
1
0
0
0
1
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0

0
1
1
0
10
1
2
3
4
5
6
7
1
3
4
5
7
7
4
2 3
6
successors









36 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #6. THE GRAPH DATA MODEL
6.3 CONNECTED COMPONENTS OF AN UNDIRECTED GRAPH
We can divide any undirected graph into one or more connected components. Each con-
nected component is a set of nodes with paths from any member of the component to
any other. Morover, the connected components are maximal, that is, for no node in the
component there is a path to any node outside the component.
If a graph consists of a single connected component, then we say that the graph is
connected. By the definition, our graph is connected if we replace arcs by edges.
We now present a way to construct the connected components of a graph G. Just
begin with a graph G
0
consisting of the nodes of G with none of the edges. Then con-
sider the edges of G, one at a time, to construct a sequence of graphs G
0
, G
1
, . . . ,
where G
i
consists of the nodes of G and the first i edges of G.
BASIS. G
0
consists of only the nodes of G with none of the edges. Every node is in a
component by itself.
INDUCTION. Suppose we have the connected components for the graph G
i
after con-
sidering the first i edges, and we now consider the (i + 1)st edge, {u, v}.
1. If nodes u and v are in the same component of G
i

, then G
i+1
has the same set of
connected components as G
i
, because the new edge does not connect any nodes
that were not already connected.
2. If nodes u and v are in different components, we merge the components contain-
ing u and v to get the connected components for G
i+1
. The figure on the next page
2
3
1
5
7
6
4
0
6.4 GLOSSARY 37
suggests why there is a path from any node x in the component of u, to any node
y in the component of v. We follow the path in the first component from x to u,
then the edge {u, v}, and finally the path from v to y that we know exists in the
second component.
When we have considered all edges in this manner, we have the connected components
of the full graph.
6.4 GLOSSARY
Graph: Đồ thò.
Directed graph: Đồ thò có hướng.
Undirected graph: Đồ thò vô hướng.

Arc: Cung.
Edge: Cạnh.
Head: Đầu.
Tail: Đuôi.
Predecessor: Tiền nhiệm.
Successor: Kế vò.
Loop: Vòng khuyên.
Cycle: Chu trình.
Cyclic: có vòng.
Acyclic: không vòng.
Path: Đường đi.
v
y
x
u
38 INTRODUCTION TO COMPUTER SCIENCE: HANDOUT #6. THE GRAPH DATA MODEL
Neighbor: Lân cận.
Adjacency List: Danh sách kề.
Adjacency Matrix: Ma trận kề.
Connected Component: Thành phần liên thông.
Basis: Cơ sở, bước cơ sở.
Induction: Quy nạp, bước quy nạp.

×