0% found this document useful (0 votes)
9 views

Module 4

The document discusses binary search trees and their properties and operations. It describes how to search, insert, and delete nodes from a binary search tree. It also covers related topics like selection trees, forest representations, and set representations using disjoint sets.

Uploaded by

vaishnavirai273
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module 4

The document discusses binary search trees and their properties and operations. It describes how to search, insert, and delete nodes from a binary search tree. It also covers related topics like selection trees, forest representations, and set representations using disjoint sets.

Uploaded by

vaishnavirai273
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 83

Module 4

Binary Search Tree


 Heap
– a min (max) element is deleted. O(log2n)
– deletion of an arbitrary element O(n)
– search for an arbitrary element O(n)
 Binary search tree
– Every element has a unique key.
– The keys in a nonempty left subtree (right subtree) are
smaller (larger) than the key in the root of subtree.
– The left and right subtrees are also binary search trees.

CHAPTER 5 2
Examples of Binary Search Trees

20 30 60

12 25 5 40 70

10 15 22 2 65 80

CHAPTER 5 3
Searching a Binary Search Tree
tree_pointer search(tree_pointer root,
int key)
{
/* return a pointer to the node that contains key. If
there is no such
node, return NULL */

if (!root) return NULL;


if (key == root->data) return root;
if (key < root->data)
return search(root->left_child,
key);
return search(root->right_child,key);
}

CHAPTER 5 4
Another Searching Algorithm
tree_pointer search2(tree_pointer tree, int key)
{
while (tree) {
if (key == tree->data) return tree;
if (key < tree->data)
tree = tree->left_child;
else tree = tree->right_child;
}
return NULL;
}

O(h)
CHAPTER 5 5
Insert Node in Binary Search Tree

30 30 30

5 40 5 40 5 40

2 2 80 2 35 80

Insert 80 Insert 35

CHAPTER 5 6
Insertion into A Binary Search Tree
void insert_node(tree_pointer *node, int num)
{tree_pointer ptr,
temp = modified_search(*node, num);
if (temp || !(*node)) {
ptr = (tree_pointer) malloc(sizeof(node));
if (IS_FULL(ptr)) {
fprintf(stderr, “The memory is full\n”);
exit(1);
}
ptr->data = num;
ptr->left_child = ptr->right_child = NULL;
if (*node)
if (num<temp->data) temp->left_child=ptr;
else temp->right_child = ptr;
else *node = ptr;
}
}

CHAPTER 5 7
Deletion for A Binary Search Tree
1
leaf 30
node
5 80
T1 T2
1
2
2
T1 X

T2
CHAPTER 5 8
Deletion for A Binary Search Tree
non-leaf
40 40
node

20 60 20 55

10 30 50 70 10 30 50 70

45 55 45 52

52
Before deleting 60 After deleting 60

CHAPTER 5 9
1

2
T1

T2 T3
1

2‘
T1

T2’ T3
CHAPTER 5 10
Selection Trees
(1) winner tree
(2) loser tree

CHAPTER 5 11
sequential
allocation
winner tree Each node represents
the smaller of its two
1
scheme children.
6
(complete
2 3
binary
tree) 6 8
4 5 6 7
9 6 8 17

8 9 10 11 12 13 14 15
10 9 20 6 8 9 90 17
18
sequence
ordered

15 20 20 15 15 11 100
16 38 30 25 50 16 110 20

run 1 run 2 run 3 run 4 run 5 run 6 run 7 run 8


CHAPTER 5 12
*Figure 5.35: Selection tree of Figure 5.34 after one record has been
output and the tree restructured(nodes that were changed are ticked)

1 

8
2  3
9 8

4 5  6 7
9 15 8 17
8 9 10 11 12 13 14 15
10 9 20 15 8 9 90 17

15 20 20 25 15 11 100 18
16 38 30 25 50 16 110 20
Analysis
 K: # of runs
 n: # of records
 setup time: O(K) (K-1)
 restructure time: O(log2K) log2(K+1)
 merge time: O(nlog2K)
 slight modification: tree of loser
– consider the parent node only (vs. sibling nodes)
CHAPTER 5 14
*Figure 5.36: Tree of losers corresponding to Figure 5.34 (p.235)
overall
6 winner
8
9
1
8

2
9 3
9 15 17

4 5 6 7
10 20 15 9 90

8 9 10 11 12 13 14 15
10 9 20 6 8 9 90 17
Run 1 2 3 4 5 6 7 8
15 15
Forest
 A forest is a set of n >= 0 disjoint trees
A
Forest
A E G
B E

B C D F H I C F G

D H

CHAPTER 5 16
Transform a forest into a binary tree

 T1, T2, …, Tn: a forest of trees


B(T1, T2, …, Tn): a binary tree
corresponding to this forest
 algorithm
(1) empty, if n = 0
(2) has root equal to root(T1)
has left subtree equal to B(T11,T12,…,T1m)
has right subtree equal to B(T2,T3,…,Tn)

CHAPTER 5 17
Forest Traversals
 Preorder
– If F is empty, then return
– Visit the root of the first tree of F
– Taverse the subtrees of the first tree in tree preorder
– Traverse the remaining trees of F in preorder
 Inorder
– If F is empty, then return
– Traverse the subtrees of the first tree in tree inorder
– Visit the root of the first tree
– Traverse the remaining trees of F is indorer

CHAPTER 5 18
A inorder: EFBGCHIJDA
preorder: ABEFCGDHIJ
B
A

E C
B C D

F G D
E F J
G H I
H

I
B C
r
preorde
D
J E G H
F I
J

CHAPTER 5 19
Set Representation
 S1={0, 6, 7, 8}, S2={1, 4, 9}, S3={2, 3, 5}
0 1 2

6 7 8 4 9 3 5
Si  Sj = 
 Two operations considered here
– Disjoint set union S1  S2={0,6,7,8,1,4,9}
– Find(i): Find the set containing the element i.
3  S3, 8  S1
CHAPTER 5 20
Disjoint Set Union
Make one of trees a subtree of the other

0 1

8 1 0 4 9
6 7

4 9 6 7 8

Possible representation for S1 union S2

CHAPTER 5 21
*Figure 5.41:Data Representation of S1S2and S3 (p.240)

0
set pointer
name
6 7 8
S1
S2 4

S3 1 9

3 5
Array Representation for Set
i [0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
parent -1 4 -1 2 -1 2 0 0 0 4

int find1(int i)
{
for (; parent[i]>=0; i=parent[i]);
return i;
}

void union1(int i, int j)


{
parent[i]= j;
}

CHAPTER 5 23
*Figure 5.43:Degenerate tree (p.242)

union operation n-1 union(0,1), find(0)


O(n) n-1 union(1,2), find(0)
.
find operation n-2 .
O(n2) n .
i union(n-2,n-1),find(0)
i 2 

degenerate tree
*Figure 5.44:Trees obtained using the weighting rule(p.243)
weighting rule for union(i,j): if # of nodes in i < # in j then j the parent of i
Modified Union Operation
void union2(int i, int j)
{ Keep a count in the root of tree
int temp = parent[i]+parent[j];
if (parent[i]>parent[j]) {
parent[i]=j;
i has fewer nodes.
parent[j]=temp;
}
else {
j has fewer nodes
parent[j]=i;
parent[i]=temp;
}
} If the number of nodes in tree i is
less than the number in tree j, then
make j the parent of i; otherwise
make i the parent of j.
CHAPTER 5 26
Figure 5.45:Trees achieving worst case bound (p.245)

 log28+1
Modified Find(i) Operation
int find2(int i)
{
int root, trail, lead;
for (root=i; parent[root]>=0;
root=parent[root]);
for (trail=i; trail!=root;
trail=lead) {
lead = parent[trail];
parent[trail]= root;
}
return root:
} If j is a node on the path from
i to its root then make j a child
of the root

CHAPTER 5 28
0 0

1 2 4 1 2 4 6 7

3 5 6 3 5

find(7) find(7) find(7) find(7) find(7) find(7) find(7) find(7)


go up 3 1 1 1 1 1 1 1
reset 2
12 moves (vs. 24 moves)

CHAPTER 5 29
Applications
 Find equivalence class i  j
 Find Si and Sj such that i  Si and j  Sj
(two finds)
– Si = Sj do nothing
– Si  Sj union(Si , Sj)
 example
0  4, 3  1, 6  10, 8  9, 7  4, 6  8,
3  5, 2  11, 11  0
{0, 2, 4, 7, 11}, {1, 3, 5}, {6, 8, 9, 10}

CHAPTER 5 30
preorder: ABCDEFGHI
inorder: BCAEDGHFI
A A

B, C D, E, F, G, H, I B D

A
C E F

B D, E, F, G, H, I G I

C H

CHAPTER 5 31
Graph - Definition
 A graph G consists of two sets
– a finite, nonempty set of vertices V(G)
– a finite, possible empty set of edges E(G)
– G(V,E) represents a graph
 An undirected graph is one in which the pair of
vertices in a edge is unordered, (v 0, v1) = (v1,v0)
 A directed graph is one in which each edge is a
directed pair of vertices, <v0, v1> != <v1,v0>
tail head

CHAPTER 6 32
Examples for Graph
0 0 0

1 2 1 2
1
3
3 4 5 6
G1 2
G2
complete graph incomplete graph 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>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
CHAPTER 6 33
Complete Graph
 A complete graph is a graph that has the
maximum number of edges
– for undirected graph with n vertices, the maximum
number of edges is n(n-1)/2
– for directed graph with n vertices, the maximum
number of edges is n(n-1)
– example: G1 is a complete graph

CHAPTER 6 34
Adjacent and Incident
 If (v0, v1) is an edge in an undirected graph,
– v0 and v1 are adjacent
– The edge (v0, v1) is incident on vertices v0 and v1
 If <v0, v1> is an edge in a directed graph
– v0 is adjacent to v1, and v1 is adjacent from v0
– The edge <v0, v1> is incident on v0 and v1

CHAPTER 6 35
*Figure 6.3:Example of a graph with feedback loops and a
multigraph (p.260)
0

0 2 1 3

1 2
self edge multigraph:
(a) (b) multiple occurrences
of the same edge

Figure 6.3
CHAPTER 6 36
Subgraph and Path
 A subgraph of G is a graph G’ such that V(G’)
is a subset of V(G) and E(G’) is a subset of E(G)
 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
 The length of a path is the number of edges on it

CHAPTER 6 37
Figure 6.4: subgraphs of G1 and G3 (p.261)
0 0 0 1 2 0

1 2 1 2 3 1 2
3
3
G1 (i) (ii) (iii) (iv)
(a) Some of the subgraph of G1

0 0 0 0
0
單一 1 1 1
1 分開
2 2
(i) (ii) (iii) (iv)
2 (b) Some of the subgraph of G3

G3 CHAPTER 6 38
Simple Path and Style
 A simple path is a path in which all vertices,
except possibly the first and the last, are distinct
 A cycle is a simple path in which the first and
the last vertices are the same
 In an undirected graph G, two vertices, v0 and v1, are
connected if there is a path in G from v0 to v1
 An undirected graph is connected if, for every
pair of distinct vertices vi, vj, there is a path
from vi to vj

CHAPTER 6 39
connected

0 0

1 2 1 2
3
3 4 5 6
G1
G2
tree (acyclic graph)

CHAPTER 6 40
Connected Component
 A connected component of an undirected graph
is a maximal connected subgraph.
 A tree is a graph that is connected and acyclic.
 A directed graph is strongly connected if there
is a directed path from vi to vj and also
from vj to vi.
 A strongly connected component is a maximal
subgraph that is strongly connected.
CHAPTER 6 41
*Figure 6.5: A graph with two connected components (p.262)
connected component (maximal connected subgraph)

H1 0 H2 4

2 1 5

3 6

G4 (not connected)
CHAPTER 6 42
*Figure 6.6: Strongly connected components of G3 (p.262)

strongly connected component


not strongly connected (maximal strongly connected subgraph)

0
0 2

1
2
G3

CHAPTER 6 43
Degree
 The degree of a vertex is the number of edges
incident to that vertex
 For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of edges
that have v as the tail
– if di is the degree of a vertex i in a graph G with n
verticesn 1
and e edges, the number of edges is
e( d )/ 2
0
i

CHAPTER 6 44
undirected graph
degree
3 0
0 2
1 2
3 1 23 3 3
3 4 5 6
3
G13 1 1 G2 1 1
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2

2 in: 1, out: 0
GCHAPTER
3 6 45
ADT for Graph
structure Graph is
objects: a nonempty set of vertices and a set of undirected edges, where each
edge is a pair of vertices
functions: for all graph  Graph, v, v1 and v2  Vertices
Graph Create()::=return an empty graph
Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no
incident edge.
Graph InsertEdge(graph, v1,v2)::= return a graph with new edge
between v1 and v2
Graph DeleteVertex(graph, v)::= return a graph in which v and all edges
incident to it are removed
Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2)
is removed
Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE
else return FALSE
List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v

CHAPTER 6 46
Graph Representations
 Adjacency Matrix
 Adjacency Lists
 Adjacency Multilists

CHAPTER 6 47
Adjacency Matrix
 Let G=(V,E) be a graph with n vertices.
 The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
 If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
 If there is no such edge in E(G), adj_mat[i][j]=0
 The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
CHAPTER 6 48
Examples for Adjacency Matrix
0 0 4
0
2 1 5
1 2
3 6
3 1
0 1 1 1 0 1 0
1 0 1 1    7
  1 0 1 
2 0 1 1 0 0 0 0 0
1 1 0 1 0 0 0 1
   0 0 1 0 0 0 0
1 1 1 0
1 0 0 1 0 0 0 0
G2  
G1
0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0
 
0 0 0 0 1 0 1 0
symmetric 0 0 0 0 0 1 0 1
 
undirected: n2/2 0 0 0 0 0 0 1 0
directed: n2 CHAPTER 6
G4 49
Merits of Adjacency Matrix
 From the adjacency matrix, to determine the
connection of vertices is easy n 1

 The degree of a vertex is  adj _ mat[i][ j ]


j 0
 For a digraph, the row sum is the out_degree,
while the column sum is the in_degree
n 1 n 1
ind (vi )   A[ j , i ] outd (vi )   A[i , j ]
j 0 j 0

CHAPTER 6 50
Data Structures for Adjacency Lists
Each row in adjacency matrix is represented as an adjacency list.

#define MAX_VERTICES 50
typedef struct node *node_pointer;
typedef struct node {
int vertex;
struct node *link;
};
node_pointer graph[MAX_VERTICES];
int n=0; /* vertices currently in use *
CHAPTER 6 51
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
CHAPTER 6 52
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
Interesting Operations
degree of a vertex in an undirected graph
–# of nodes in adjacency list
# of edges in a graph
–determined in O(n+e)
out-degree of a vertex in a directed graph
–# of nodes in its adjacency list
in-degree of a vertex in a directed graph
–traverse the whole data structure

CHAPTER 6 53
Compact Representation
0 4
node[0] … node[n-1]: starting point for vertices
2 1 5 node[n]: n+2e+1
3 6 node[n+1] … node[n+2e]: head node of edge

7
[0] 9 [8] 23 [16] 2
[1] 11 0 [9] 1 4 [17] 5
[2] 13 [10] 2 5 [18] 4
[3] 15 1 [11] 0 [19] 6
[4] 17 [12] 3 6 [20] 5
[5] 18 2 [13] 0 [21] 7
[6] 20 [14] 3 7 [22] 6
[7] 22 3 [15] 1
CHAPTER 6 54
Figure 6.10: Inverse adjacency list for G3

0
0  1 NULL

1 1  0 NULL

2  1 NULL
2

Determine in-degree of a vertex in a fast way.

CHAPTER 6 55
Figure 6.11: Alternate node structure for adjacency lists (p.267)

tail head column link for head row link for tail

CHAPTER 6 56
Figure 6.12: Orthogonal representation for graph G3(p.268)

0 1 2

0 0 1 NULL NULL

1 1 0 NULL 1 2 NULL NULL

0
2 NULL
0 1 0
 
 1 0 1 
0 0 0
1

2
CHAPTER 6 57
Figure 6.13:Alternate order adjacency list for G1 (p.268)
Order is of no significance.
headnodes vertax link

0  3  1  2 NULL

1  2  0  3 NULL

2  3  0  1 NULL

3  2  1  0 NULL

1 2
3 CHAPTER 6 58
Adjacency Multilists
An edge in an undirected graph is
represented by two nodes in adjacency list
representation.
Adjacency Multilists

–lists in which nodes may be shared among


several lists.
(an edge is shared by two different paths)

marked vertex1 vertex2 path1 path2

CHAPTER 6 59
Example for Adjacency Multlists
Lists: vertex 0: M1->M2->M3, vertex 1: M1->M4->M5
vertex 2: M2->M4->M6, vertex 3: M3->M5->M6
(1,0)
0 N1 0 1 N2 N4 edge (0,1)
1 (2,0)
2 N2 0 2 N3 N4 edge (0,2)
(3,0)
3 0 3 N5
N3 (2,1)
edge (0,3)
0 N4 1 2 N5 N6 edge (1,2)
(3,1)
N5 1 3 N6 edge (1,3)
1 2 (3,2)
N6 2 3 edge (2,3)
3
six edges
CHAPTER 6 60
Adjacency Multilists

typedef struct edge *edge_pointer;


typedef struct edge {
short int marked;
int vertex1, vertex2;
edge_pointer path1, path2;
};
edge_pointer graph[MAX_VERTICES];

marked vertex1 vertex2 path1 path2

CHAPTER 6 61
Some Graph Operations
 Traversal
Given G=(V,E) and vertex v, find all wV,
such that w connects v.
– Depth First Search (DFS)
preorder tree traversal
– Breadth First Search (BFS)
level order tree traversal
 Connected Components
 Spanning Trees

CHAPTER 6 62
*Figure 6.19:Graph G and its adjacency lists (p.274)
depth first search: v0, v1, v3, v7, v4, v5, v2, v6

breadth first search: v0, v1, v2, v3, v4, v5, v6, v7
CHAPTER 6 63
Depth First Search
#define FALSE 0
#define TRUE 1
short int visited[MAX_VERTICES];
void dfs(int 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); Data structure
} adjacency list: O(e)
adjacency matrix: O(n2)
CHAPTER 6 64
Breadth First Search
typedef struct queue *queue_pointer;
typedef struct queue {
int vertex;
queue_pointer link;
};
void addq(queue_pointer *,
queue_pointer *, int);
int deleteq(queue_pointer *);

CHAPTER 6 65
Breadth First Search (Continued)
void bfs(int v)
{
node_pointer w;
queue_pointer front, rear;
front = rear = NULL; adjacency list: O(e)
printf(“%5d”, v); adjacency matrix: O(n2)
visited[v] = TRUE;
addq(&front, &rear, v);

CHAPTER 6 66
while (front) {
v= deleteq(&front);
for (w=graph[v]; w; w=w->link)
if (!visited[w->vertex]) {
printf(“%5d”, w->vertex);
addq(&front, &rear, w->vertex);
visited[w->vertex] = TRUE;
}
}
}
CHAPTER 6 67
Connected Components
void connected(void)
{
for (i=0; i<n; i++) {
if (!visited[i]) {
dfs(i);
printf(“\n”);
}
adjacency list: O(n+e)
} adjacency matrix: O(n2)
}
CHAPTER 6 68
Spanning Trees
 When graph G is connected, a depth first or
breadth first search starting at any vertex will
visit all vertices in G
 A spanning tree is any tree that consists solely
of edges in G and that includes all the vertices
 E(G): T (tree edges) + N (nontree edges)
where T: set of edges used during search
N: set of remaining edges

CHAPTER 6 69
Examples of Spanning Tree

0 0 0 0

1 2 1 2 1 2 1 2
3 3 3 3

G1 Possible spanning trees

CHAPTER 6 70
Spanning Trees
 Either dfs or bfs can be used to create a
spanning tree
– When dfs is used, the resulting spanning tree is
known as a depth first spanning tree
– When bfs is used, the resulting spanning tree is
known as a breadth first spanning tree
 While adding a nontree edge into any spanning
tree, this will create a cycle
CHAPTER 6 71
DFS VS BFS Spanning Tree
0 0 0

1 2 1 2 1 2

3 4 5 6 3 4 5 6 3 4 5 6

7 7 nontree edge 7
cycle
DFS Spanning BFS Spanning

CHAPTER 6 72
A spanning tree is a minimal subgraph, G’, of G
such that V(G’)=V(G) and G’ is connected.

Any connected graph with n vertices must have


at least n-1 edges.

A biconnected graph is a connected graph that has


no articulation points. 0

1 2
biconnected graph
3 4 5 6

7
CHAPTER 6 73
0 8 9

1 7

3 5 connected graph

2
4 6
two connected components one connected graph

0 8 9 0 8 9

7 1 7
1

3 5 3 5

2 2
4 6 4 6
CHAPTER 6 74
biconnected component: a maximal connected subgraph H
(no subgraph that is both biconnected and properly contains H)

0 8 9

0 8 9
1 7 7

1 7
1 7

3 5
3 3 5 5
2
4 6 2
4 6

biconnected components
CHAPTER 6 75
Find biconnected component of a connected undirected graph
by depth first spanning tree
nontree
0 edge
3 (back edge)
4 0 9 8 9 8 1 5
nontree 4 5
7 7edge
3 1
0 5 (back edge) 2 2 6 6
2 2 3 5 3
1 7 7
dfn
depth 8 9
4 6
first 4 0 9 8
1 6
number Why is cross edge impossible?
(a) depth first spanning tree (b)
If u is an ancestor of v then dfn(u) < dfn(v).
CHAPTER 6 76
*Figure 6.24: dfn and low values for dfs spanning tree with root =3(p.281)

Vertax 0 1 2 3 4 5 6 7 8 9
dfn 4 3 2 0 1 5 6 7 9 8
low 4 0 0 0 0 5 5 5 9 8

CHAPTER 6 77
0
3 *The root of a depth first spanning
1 5 tree is an articulation point iff
4 5 it has at least two children.

2 2 6 6 *Any other vertex u is an articulation


3 point iff it has at least one child w
1 7 7 such that we cannot reach an ancestor
8 9of u using a path that consists of
4 0 9 8
(1) only w (2) descendants of w (3)
low(u)=min{dfn(u), single back edge.
min{low(w)|w is a child of u},
min{dfn(w)|(u,w) is a back edge}

u: articulation point
low(child)  dfn(u)
CHAPTER 6 78
vertex dfn low child low_child low:dfn
0 4 4 (4,n,n) null null null:4
1 3 0 (3,4,0) 0 4 43 
2 2 0 (2,0,n) 1 0 0<2
3 0 0 (0,0,n) 4,5 0,5 0,5  0 
4 1 0 (1,0,n) 2 0 0<1
5 5 5 (5,5,n) 6 5 55 
6 6 5 (6,5,n) 7 5 5<6
7 7 5 (7,8,5) 8,9 9,8 9,8  7 
8 9 9 (9,n,n) null null null, 9
9 8 8 (8,n,n) null null null, 8
3
1 5
4 5

2 2 6 6
3
1 7 7
8 9
4 0 9 8
CHAPTER 6 79
*Program 6.5: Initializaiton of dfn and low (p.282)

void init(void)
{
int i;
for (i = 0; i < n; i++) {
visited[i] = FALSE;
dfn[i] = low[i] = -1;
}
num = 0;
}

CHAPTER 6 80
*Program 6.4: Determining dfn and low (p.282)
void dfnlow(int u, int v) Initial call: dfn(x,-1)
{
/* compute dfn and low while performing a dfs search
beginning at vertex u, v is the parent of u (if any) */
node_pointer ptr;
int w;
dfn[u] = low[u] = num++; low[u]=min{dfn(u), …}
for (ptr = graph[u]; ptr; ptr = ptr ->link) {
v w = ptr ->vertex;
v if (dfn[w] < 0) { /*w is an unvisited vertex */
u dfnlow(w, u);
u low[u] = MIN2(low[u], low[w]);
} low[u]=min{…, min{low(w)|w is a child of u}, …}
w
else if (w != v) dfn[w]0 非第一次,表示藉 back edge
O X low[u] =MIN2(low[u], dfn[w] );
}
CHAPTER 6 81
} low[u]=min{…,…,min{dfn(w)|(u,w) is a back edge}
*Program 6.6: Biconnected components of a graph (p.283)
void bicon(int u, int v)
{
/* compute dfn and low, and output the edges of G by their
biconnected components , v is the parent ( if any) of the u
(if any) in the resulting spanning tree. It is assumed that all
entries of dfn[ ] have been initialized to -1, num has been
initialized to 0, and the stack has been set to empty */
node_pointer ptr;
int w, x, y;
dfn[u] = low[u] = num ++; low[u]=min{dfn(u), …}
for (ptr = graph[u]; ptr; ptr = ptr->link) {
w = ptr ->vertex; (1) dfn[w]=-1 第一次
if ( v != w && dfn[w] < dfn[u] ) (2) dfn[w]!=-1 非第一次,藉 back
add(&top, u, w); /* add edge to stack */ edge

CHAPTER 6 82
if(dfn[w] < 0) {/* w has not been visited */
bicon(w, u); low[u]=min{…, min{low(w)|w is a child of u}, …
low[u] = MIN2(low[u], low[w]);
if (low[w] >= dfn[u] ){ articulation point
printf(“New biconnected component: “);
do { /* delete edge from stack */
delete(&top, &x, &y);
printf(“ <%d, %d>” , x, y);
} while (!(( x = = u) && (y = = w)));
printf(“\n”);
}
}
else if (w != v) low[u] = MIN2(low[u], dfn[w]);
} low[u]=min{…, …, min{dfn(w)|(u,w) is a back edge}}
}

CHAPTER 6 83

You might also like