You are on page 1of 89

TE

&

&

( )

.
2004

&

&

1.1
1.1.1
1.1.2
1.2
1.2.1
1.2.2
1.2.3
1.3
1.3.1
1.3.2
1.3.3
1.3.4

8
8
9
10
10
11
12
14
14
14
15
16

2.1
2.2
2.3
2.4

20
21
24
25

3.1
3.2
3.3

28
28
30

4.1
4.2
4.3

32
32
33

5.1
5.2
5.3
5.4
5.5

38
38
39
40
40

&

6

6.1
6.2
6.2.1
6.2.2
6.3
6.3.1
6.3.2
6.3.3

44
44
44
46
48
48
51
53

7.1
7.2
7.3 B-trees
7.4 Tries

56
58
63
66

8.1
8.2
8.3
8.3.1
8.3.2
8.4

74
76
78
80
80
81

9

9.1
9.2
9.3
9.4

84
85
86
86

88

89

&



, .
,
, , .
, ,
,
,
.


. ,

.

.

.
, .

.
,

.

.

, .

.

.
5

&

.
.
.
, .
,
.

.
.
,
, .
,
.

:

, ,
.

&

1.1
1.1.1

1.1.2

1.2
1.2.1

1.2.2

1.2.3

1.3
1.3.1

1.3.2

1.3.3

1.3.4

&
1.1
,
, .
:

(input). K,
.
,

.

(output).
.

(definiteness).
. ..
, .

(finiteness).
.

(effectiveness).

.
, .
1.1.1

.
.

, . .

. ,

, , .
.

. ,
.
,

&
.
:

. ,


.

1.1.2

(constants).

.

(variables). ,
.
,
.
,

&
o
o

o .

(operators).

.
o : +, -, *, /, %
o :
o

>, <, =, >=, <=, !=


(&&), (||), (!)

(expressions).
(operands), .

.
.

.

1.2
1.2.1
( )
,
.
, .
..
, .

scanf(%d,&a);

scanf(%d,&b);

c=a+b

c = a + b;

printf(%d,c);

10

C:

&
a b
:
c=a+b
(assignment statement).
:
=

, , .
1.2.2

/ .
.


.

( )
.

. :

if (expression)
Statements;

if (expression)
Statements 1;
else
Statements 2;

11

&
E
1
2
1

3
3

if (expression 1)
if (expression 2)
Statements 1;
else
Statements 2;
else
if (expression 3)
Statements 3;
else
Statements 4;


.
.

.
.
,
.
:

1.2.3
,
.

, .

.
, .
12

&

while (expression)
Statements;

do
Statements;
while (expression);


for (variable = initial value; variable<= final value; step)
Statements;

(while)
.
(do-while)
,
, .
(for)
.

13

&
1.3

.
,
.
1.3.1

.
. ,
.

.

.
,
. , ,

. ,
, :

1.3.2
,
,
.

.
.
,

14

&
.
:

1.3.3

. ,
, (processing
time) (memory space).
:

, .
.
n, ,

.
.
,
f(n),

(time complexity) g(n),


(space complexity).
, , ,
, .
, (O notation),
order.

:

15

&

(1)

(log n)

(n)
.

n .

(n.log n)

.

(n2)
.
.

(n3)
.
.

(2n)
.
.

1.3.4

. ,
,
.
.
, , / ,
. /
, ,
.
, .

16

&
(parallel)
.
,

. , ,
.
(optimal)
, .
(polynomial)
. ,
O(n), O(n2), .

(2n), O(n.2n), . .

( ) ,
, .
(intractable). ..
(traveling
salesman),
.

(approximate) ,
.
(heuristic)
,
.

. ,
,
.

17

&

18

&

2.1
2.2
2.3
2.4

19

&
2.1
,
.
.
,
.
+ =
/,
.
, .
(data structure)
.


.
(nodes).
:

(access)

(insertion)

(deletion)

(searching)
,

(sorting)

20

(copying)

&

(merging)

(separation)

.

, ,
, .
,
.
2.2

,

.

.
,
.
(
, .).

.

(indexes) .
,
, - .

21

&
1
.
1

n
.

_
table[n], i, min
min = table[1]
i 2 n
A table[i] < min
min = table[i]

min
_
0

11

23

34

56

65

25

C:
#define N 10

main()
{
int table[N] = {11, 23, 2, 34, 56, 65, 7, 9, 1, 25};
int i, min;
min = table[0];
for (i=1; i<N; i++)
if (table[i]<min)
min = table[i];
printf(Min = %d,min);
}
2
.
__
table[m][n], i, j, sum, row[m], col[n]
i 1 m
j 1 n
table[i][j]

22

&
sum = 0;
i 1 m
row[i] = 0

j 1 n
col[j] = 0

i 1 m
j 1 n
sum = sum + table[i][j]
row[i] = row[i] + table[i][j]
colo[j] = col[j] + table[i][j]


sum
i 1 m
row[i]
T
j 1 n
col[i]
T
__
C:
#define M 5
#define N 5
main()
{
int table[M][N], row[M], col[N];
int i, j, sum;
for (i=0; i<M; i++)
for (j=0; j<N; j++)
scanf(%d,&table[i][j]);
sum = 0;
for (i=0; i<M; i++)
row[i] = 0;
for (j=0; j<N; j++)
col[j] = 0;
23

&
for (i=0; i<M; i++)
for (j=0; i<N; i++)
{
sum = sum + table[i][j];
row[i] = row[i] + table[i][j];
col[j] = col[j] + table[i][j];
}
printf(sum = %d, sum);
for (i=0; i<M; i++)
printf(row[%d] = %d\n, i, row[i]);
for (j=0; j<N; j++)
printf(col[%j] = %d\n, j, col[j]);
}
table

col

row

16

21

53

28

38

13

51

139

17

67

22

40

30

176

20

40

10

13

86

21

34

48

29

26

158

90

166

123

106

127

612

sum

2.3
p[N]
. /
loc(p[lowerbound]), lowerbound
. 0.
i- p
loc(p[i]) = loc(p[lowerbound]) + i lowerbound

/.
p[M][N]

24

(row-major order)

(column-major order)

&
,
m- .
,
n- .
p[i][j] :
loc(p[i][j]) = loc(p[0][0]) + i + M*j
2.4

.

.

(symmetric)
.
n(n+1)/2 .

(triangular)
0.
n(n+1)/2

(tridiagonal)

0. 3*n-2

(sparse)
0
( > 80%).
. ..
.

. n 3*n
.

25

&

26

&

3.4
3.5
3.6

27

&
3.1
, ,
.
.
.

.

. ,

. ,


, .
,
.
3.2

:
n! = 1*2*3*..(n-1)*n

:
int factorial(int n)
{
int i,f;
f = 1;
for (i=2; i<=n; i++)
f = f*i;
return f;
}

:
n! = 1
n = 0,
= n*(n-1)! n > 0

28

&
:
int factorial(int n)
{
int f;
if (n = = 0)
f = 1;
else
f = n*factorial(n-1);
return f;
}

, .. n = 4:
:
f=1
i=2
f = 1*2 = 2
i=3
f = 2*3 = 6
i=4
f = 6*4 = 24
:
factorial(4) = 4*factorial(3)
factorial(3) = 3*factorial(2)
factorial(2) = 2*factorial(1)
factorial(1) = 1*factorial(0)
factorial(0) = 1
, 1
factorial(1) = 1.
factorial(2) = 2*1 = 2
factorial(3) = 3*2 = 6
factorial(4) = 4*6 = 24

29

&
3.3

:
xn = x*x*x*..*x, n

:
int power(int x, int n)
{
int i,p;
p = 1;
for (i=1; i<=n; i++)
p = p*x;
return p;
}

:
xn = 1
= x* xn-1

n = 0,
n > 0

:
int power(int x, int n)
{
int p;
if (n = = 0)
p = 1;
else
p = x*power(x,n-1);
return p;
}

30

&

4.1
4.2
4.3

31

&
4.1
(searching)
.

.

(.. , , , .).

:

(Sequential search)

(Binary search)

4.2

.
.
, .
, .
( ) ,
, (
0).
1)
__1
p[N], i, key, found, position
found = false
position = -1
i=0
(found = = false ) (i < N)
AN p[i] = = key TOTE
found = true
position = i

i = i+1


AN found = = true
position


__1

32

&
2)
__2
p[N], i, key, found, position
i > N
found = false

N p[i] = = key TOTE


found = true
position = i

i = i+1
__2(p,key,found,position)


__2
AN found = = true
position



4.3
,
,
.
. ,
. , . ,

, .

. .

, , ...... .

= log2N
,
.
.
( ) ,
, .

33

&
1)
__1
p[N], mid, left, right, key, found, position
found = false
position = -1
left = 0
right = N-1
(found = = false ) (left <= right)
mid = (left+right) / 2
AN key = = p[mid] TOTE
found = true
position = mid

AN key < p[mid] TOTE


right = mid-1
A
left = mid+1



AN found = = true
position



__1
2)
__2
p[N], mid, left, right, key, position
left > right
position = -1

mid = (left + right) / 2


N p[mid] = = key TOTE
position = mid

AN key < p[mid]


position = __2(left, mid-1, p, key)

position = __2(mid+1, right, p, key)




__2
AN position = = -1

position

34

&

10

100

1000

10

10000

14

100000

17

1000000

20

10000000

24

100000000

27

1000000000

30

35

&

36

&

5.1
5.2
5.3
5.4
5.5

37

&
5.1

. (ascending)
(descending).
.
,
, ,

.

.
. , :


(performance analysis).

5.2 (Straight Selection)


:

..

38

44

55

12

42

94

18

06

67

06

55

12

42

94

18

44

67

06

12

55

42

94

18

44

67

06

12

18

42

94

55

44

67

06

12

18

42

94

55

44

67

06

12

18

42

44

55

94

67

06

12

18

42

44

55

94

67

06

12

18

42

44

55

67

94

&
C:
for (i=0; i<N-1; i++)
{
k = i;
min = p[i];
for (j = i+1; j<N; j++)
{
if (p[j] < min)
{
k = j;
min = p[j];
}
}
p[k] = p[i] ;
p[i] = min;
}
5.3 (Straight Insertion)
:

, ,
, , .

..
44

55

12

42

94

18

06

67

44

55

12

42

94

18

06

67

12

44

55

42

94

18

06

67

12

42

44

55

94

48

06

67

12

42

44

55

94

18

06

67

12

18

42

44

55

94

06

67

06

12

18

42

44

55

94

67

06

12

18

42

44

55

67

94

C:
for (i=2; i<=N; i++)
{
x = p[i];
p[0] = x;
j = i-1;
while (x < p[j])
{
p[j+1] = p[j] ;
j = j-1 ;
}
p[j+1] = x ;
}

39

&
5.4 (Bubble Sort)

, .
.
..
44

55

12

42

94

18

06

67

06

44

55

12

42

94

18

67

06

12

44

55

18

42

94

67

06

12

18

44

55

42

67

94

06

12

18

42

44

55

67

94

06

12

18

42

44

55

67

94

06

12

18

42

44

55

67

94

06

12

18

42

44

55

67

94

C:
for (i=1; i<N; i++)
for (j=N-1; j>=i; j--)
if (p[j-1] > p[j])
{
temp = p[j-1];
p[j-1] = p[j] ;
p[j] = temp ;
}
5.5 (Quick Sort)

.

.

.
,

. ,
(pivot)
,

40

&
. ,
, , .,
.
.
..
36
i

47

18

59

32
x

73

41

27

47
i

18

59

32

73

41
j

27

51
j

j
36

51

j
27

32
i

18

j
47
j

73

41

36

51

59

47

73

41

36

51

[59
i

47

73

41

36

51]
j

i
51
i

41

36

73
j

59
j

27

18
i

[27
i

18]
j

j
32
j
i
32

i
18
i

27
j
i

59

47

i
i

18

27

[59
i
36
i

47

51

41

47

51

41

i
36

47

[36
i

47

36
[36
i
j
36

i
41
41]
j

36]
j
59
j

i
73

j
i
41
i
41]
j

51
j
i
51

59

47
j
i
47

41

41

&
C:
void quicksort(int left, int right, int p[])
{
int i, j, mid, x, temp;
if (left < right)
{
i = left;
j = right;
mid = (left+right)/2;
x = p[mid];
while (i < j)
{
while (p[i] < x)
i++;
while (p[j] > x)
j--;
if (i < j)
{
if (p[i] == p[j])
{
if (i<mid)
i++;
if (j>mid)
j--;
}
else
{
temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}
}
quicksort(left,j-1,p);
quicksort(j+1,right,p);
}
}

42

&


6.1
6.2
6.2.1

6.2.2

6.3
6.3.1

6.3.2

6.3.3

43

&
6.1
(linear list) x1,
x2,......., xn xk xk+1 xk-1.
:

(sequential linear lists)

(linked linear lists)

/
.

.
, :

(static data structures)

(dynamic data structures)

, ,

.
,
.
6.2
6.2.1 (stack)
.
(top).
.
LIFO (Last In First Out)
4
3
top

44

15

12

&

. :

(push)

(pop)


, (overflow).
,
, (underflow).
C:
#define N 100
int stack[N], top = -1;
void push(int stack[],int *t,int obj)
{
if (*t = = N-1)
{
printf("Stack overflow...\n");
getch();
abort();
}
else
stack[++(*t)] = obj;
}
int pop(int stack[],int *t)
{
int r ;
if (*t < 0)
{
printf("Stack empty...\n");
getch();
abort();
}
else
r = stack[(*t)--];
return r;
}

45

&
6.2.2

(queue)

, ..
. ,
. , .

FIFO (First In First Out)
:

(enqueue)

(dequeue)

, ,
(front) (rear).
rear
, front ,
, .

front
C:
#define N 100
int q[N], front = -1, rear = -1;
void enqueue(int q[], int *r, int obj)
{
if (*r = = N-1)
{
printf("Queue is full...");
getch();
}
else
q[++(*r)] = obj;
}

46

15

12

rear

&
void dequeue(int q[], int *f, int r)
{
int x;
if (*f = = r)
printf("Queue is empty...\n");
else
{
x = q[++(*f)];
printf("%d has been deleted...",x);
}
}
. rear
, ,
front ( ).
..
0

15

12

16

front

rear


.
.
..
0

15

12

16

front

rear

elements = front rear;


first = front + 1;
for (i=0; i<elements; i++)
q[i] = q[first++];
front = -1;
rear = elements 1;

47

&
,
rear = N-1, 0.
(circular queue).
0

21

rear

15

12

16

front

6.3
,
.

.
, .
,

( ).

head

32

41

48

51

54

NULL

(structure) .
(data) ( )
(next) . (head)
,
NULL .
6.3.1
C:
struct node
{
int data;
struct node *next;
};
typedef struct node * PTR;

48

&
)
PTR list_create(PTR head)
{
PTR current;
int x;
printf(Give an integer, 0 to stop:);
scanf(%i,&x);
if (x = = 0)
return NULL;
else
{
head = malloc(sizeof(struct node));
head->data = x;
current = head;
printf(Give an integer, 0 to stop:);
scanf(%i,&x);
while (x!=0)
{
current->next = malloc(sizeof(struct node));
current = current->next;
current->data = x;
printf(Give an integer, 0 to stop:);
scanf(%i,&x);
}
current->next = NULL;
}
return head;
}
)
( )
PTR insert_to_list(PTR head, int x)
{
PTR current, previous, newnode;
int found;
newnode = malloc(sizeof(struct node));
newnode->data = x;
newnode->next = NULL;
if (head = = NULL)
head = newnode;
else
if (newnode->data < head->data)
{
newnode->next = head;
head = newnode;
}

49

&
else
{
previous = head;
current = head->next;
found = 0;
while (current != NULL && found = = 0)
{
if (newnode->data < current->data)
found = 1;
else
{
previous = current;
current = current->next;
}
}
previous->next = newnode;
newnode->next = current;
}
return head;
}
)
PTR delete_from_list(PTR head, int x)
{
PTR current, previous;
int found;
current = head;
if (current = = NULL)
{
printf("Empty list...nothing to delete.\n");
getch();
}
else
if (x = = head->data)
{
head = head->next;
free(current);
}
else
{
previous = current;
current = head->next;
found = 0;
while (current != NULL && found = = 0)
{
if (x = = current->data)
found = 1;
else

50

&
{
previous = current;
current = current->next;
}
}
if (found = = 1)
{
previous->next = current->next;
free(current);
}
else
{
printf("\nThe character is not in the list.");
getch();
}
}
return head;
}
)
void print_list(PTR head)
{
PTR current;
current = head;
if (current = = NULL)
printf("The list is empty.\n");
else
while (current != NULL)
{
printf("%i ", current->data);
current = current->next;
}
}
6.3.3

top NULL,
. push
(
). , pop, free
/
.

51

&

32

top

11

18

NULL
PTR top = NULL;
PTR push(int obj, PTR t)
{
PTR newnode;
newnode = malloc(sizeof(struct node));
newnode->data = obj;
newnode->next = t;
t = newnode;
return t;
}
PTR pop(PTR t, int *obj)
{
PTR p;
if (t = = NULL)
{
printf("Stack empty.\n");
getch();
}
else
{
p = t;
t = t->next;
*obj = p->data;
free(p);
}
return t;
}
52

&
6.3.3

front rear
NULL. front = NULL.
,
.
25

34

41

front

11

NULL

rear

PTR front = NULL, rear = NULL;


void enqueue(int obj, PTR *pf, PTR *pr)
{
PTR newnode;
newnode = malloc(sizeof(struct node));
newnode->data = obj;
newnode->next = NULL;
if ((*pf) = = NULL)
{
*pf = newnode;
*pr = newnode;
}
else
{
(*pr)->next = newnode;
*pr = newnode;
}
}
void dequeue(PTR *pf, PTR *pr)
{
PTR p;
if ((*pf) = = NULL)
printf("\nQueue empty. No elements to delete.\n");
else
{
p = *pf;
*pf = (*pf)->next;
if ((*pf) = = NULL)
*pr = *pf;
printf("\n%d has been deleted...\n",p->data);
free(p);
}
getch();
}
53

&

54

&

1.1
1.2
1.3 B-trees

55

&
7.1
,
.
, (tree).
()
.
(root),

(subtrees).

.

A
B

T = (A, B, C, D, E, F, G)
.
T1 = (B)
T2 = (C, D, E, F, G)
(node degree)
.
..
2.
C 3.
(tree degree)
.
56

&

(binary trees), 2.

(leaves) (terminal nodes).
(branches).
(father)
(children) .
(brothers).
,
(ordered).

(binary search tree).
t,
t
t.
..

D
A
B

y x
(descendant) x. , x
(ancestor) y.
1.
(depth) (height) .
(forest)
.

57

&
7.2 (Binary trees)

.
,
.
C:
struct treenode
{
char data;
struct treenode * left;
struct treenode * right;
};
typedef struct treenode * PTR;

/.
..
A*B+C
+

NULL

NULL

NULL

NULL

NULL

NULL

(traversal)
, . .
:
) (preorder traversal)
) (inorder traversal)
) (postorder traversal)

58

&
..
D
A
B

preorder
1.
2.
3.
( )
:
D, B, A, C, E, F
:
1.

(D)1

2. (B, A, C) => 1. (B)2


3. (E, F)

2. (A)

||

=>

3. (C)

1. (E)

||

2. ()

1. (C)4

3. (F)

2. ()

||

1. (A)3
2. ()
3. ()

3. ()

1. (F)6
2. ()
3. ()
inorder
1.
2.
3.
( )
:
A, B, C, D, E, F
59

&
postorder
1.
2.
3.
( )
:
A, C, B, F, E, D
C:
void preorder_traversal(PTR t)
{
if (t!=NULL)
{
printf("%c ",t->data);
preorder_traversal(t->left);
preorder_traversal(t->right);
}
}
void inorder_traversal(PTR t)
{
if (t!=NULL)
{
inorder_traversal(t->left);
printf("%c ",t->data);
inorder_traversal(t->right);
}
}
void postorder_traversal(PTR t)
{
if (t!=NULL)
{
postorder_traversal(t->left);
postorder_traversal(t->right);
printf("%c ,t->data);
}
}
:
void insert_node(PTR *pt, char x)
{
PTR t;
t = *pt;

60

&
if (t= =NULL)
{
t = malloc(sizeof (struct treenode));
t->data = x;
t->left = NULL;
t->right = NULL;
}
else
if (x<t->data)
insert_node(&(t->left),x);
else
insert_node(&(t->right),x);
*pt = t;
}
:
.
, .
.
.

, .
.
..
10

15

12

18

5.

61

&
1
, .
3.

10

15

18

12

2
, .
8.

10

15

12

18


,
, , .
(logn) O(n). ,

.
62

&
(height balanced tree)

.
(perfectly balanced tree)

.

7.3 B-trees
balanced tree ( )
binary tree.
B-tree ( ) n :

2n .

( ) n
2n .

m (1<= m <= 2n) m+1 .

63

&
..
25

10

20

13

14

15

18

22

24

26

27

28

30

40

32

35

38

41

B-tree 2 3 ( n=2).
H .
3.
2 4 .
(10,20) (30,40) 2 .
B-tree
.

2n ,
.

2n ,
. .

.


...
,
.

B-trees
.

64

42

45

46

&

1. 20
20

2. 40, 10, 30, 15


20

10

15

30

40

3. 35, 7, 26, 18, 22

10

15

18

20

30

22

26

35

40

4. 5
10

15

20

18

30

22

26

40

35

40

5. 42, 13, 46, 27, 8, 32

13

15

18

10

20

30

22

26

27

32

35

42

46

6. 38, 24, 45, 25

65

&
25

10

20

13

15

18

22

24

26

27

30

40

32

35

38

42

7.3 Tries
, (
).

. ,
, ,
.

. , .
,
.
, trie. H
tree () retrieval ().
trie, :

.
..
ALLOW
BOOK
BORROW
WAIT
,
. ,
(branch nodes), 27 . ,
(information nodes),
.
A,..,Z. ,

66

45

46

&
(
).
trie BOOK,
B .
.
. .

, . ,

.

67

&
root
A B

L
...

O
.

L
.

A
.

R
.

O
.

W X Y Z 0
.
.

I
...

W
.

T
...

O
.

..

..

68

&

( )
trie .
trie . ,
,

. ,
,
trie, .
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct branchNode
{
struct branchNode *p[26];
char *infp;
};
typedef struct branchNode * brPTR;
brPTR newBrNode(void);
brPTR addWord(char *w1, char *w2, brPTR t);
void searchWord(char *w, brPTR t);
void printTrie(brPTR t);
char engWord[10][80];
char grWord[10][80];
int n = 0;
main()
{
FILE *fp;
brPTR tr = NULL;
char w[80];
int i;
fp = fopen("lexiko.txt","r");
for (i=0; i<10; i++)
{
fscanf(fp,"%s",engWord[i]);
fscanf(fp,"%s",grWord[i]);
tr = addWord(engWord[i], grWord[i], tr);
}
fclose(fp);
69

&
printTrie(tr);
printf("\n");
printf("\nGive english word, TELOS to stop: ");
scanf("%s",w);
while (strcmp(w,"TELOS")!= 0)
{
searchWord(w,tr);
printf("\nGive english word, TELOS to stop: ");
scanf("%s",w);
}
return 0;
}
brPTR newBrNode()
{
brPTR current;
int i;
current = malloc(sizeof(struct branchNode));
for (i=0; i<26; i++)
current->p[i] = NULL;
current->infp = NULL;
return current;
}
brPTR addWord(char *w1, char *w2, brPTR t)
{
brPTR current;
int i;
if (t == NULL)
t = newBrNode();
current = t;
while (isalpha(*w1))
{
i = *w1-'A';
if (current->p[i] == NULL)
current->p[i] = newBrNode();
current = current->p[i];
w1++;
}
current->infp = w2;
return t;
}

70

&
void searchWord(char *w, brPTR t)
{
int i, flag = 1;
while (*w != '\0' && flag == 1)
{
i = *w-'A';
if (t->p[i] != NULL)
t = t->p[i];
else
flag = 0;
w++;
}
printf("
Search result = ");
if (t->infp != NULL)
printf("%s\n",t->infp);
else
printf("Word not found!\n");
}
void printTrie(brPTR t)
{
int i;
if (t->infp != NULL)
{
printf("%s = ",engWord[n++]);
printf("%s\n",t->infp);
}
for (i=0; i<26; i++)
if (t->p[i] != NULL)
printTrie(t->p[i]);
}
tries .

. , , trie
.
tries
.
.
tries
trie

71

&
. , ,
COM :
COMMAND, COMPILER, COMPUTE, COMPUTER, COMPUTING
, trie
, B-trees.
,
,
.
tries
.

72

&

8.1
8.2
8.3
8.3.1
8.3.2
8.4

73

&
8.1
.
.
(graph theory)

(, , , .).

, , ,
, .
,
, , ,
.

.

G V E. V

(vertices) . E
(edges).
V(G), E(G) G(V,E)
V, E G.
..

1
2

4
G1

11

22

44

33

55

66

77

G3

G2

- (undirected)
. ,

74

&
(v1,v2) (v2,v1) . G1, G2
-.

(directed graph)
.
<v1,v2> v1,
(tail),

v2, (head).

G3 .

V G1, G2, G3 :
V(G1) = (1,2,3,4)
V(G2) = (1,2,3,4,5,6,7)
V(G3) = (1,2,3)
E :
E(G1) = ( (1,2), (1,3), (1,4), (2,3), (2,4), (3,4) )
E(G2) = ( (1,2), (1,3), (2,4), (2,5), (3,6), (3,7) )
E(G3) = ( <1,2>, <2,1>, <2,3> )

- n (complete)

n(n 1)
.
2

.
G1 .

(v1,v2) E(G), v1 v2
(adjacent). H (v1,v2)

(incident) v1 v2 v1 v2
(independent).

(path) vm vn

[vm,.........,vn]
E(G).
, [1,2,3,4] 1
4 G1 .

(length)

. 3.

(simple)
.

75

&
..
[1,2,3,4] , [1,2,4,2] . [1,2,3,2]
G3 <3,2> E(G3).

(cycle)

.
..
[1,2,3,1] G1 .

.

(degree)

.
8.2

/.

.
.
,
.

(adjacency matrices)

(adjacency lists)

G n
N*N. To (i, j) 1
(vi, vj) E(G), 0. ,
G1, G3 :

G1

76

G3

&
C:

int graph[N][N];
:

n a, b, c, .
1, 2, 3, .
0, 1, 2, ......., n-1 .

,

( 1) ( 0).

n ,
n .
i ,
i.
..
1

NULL

NULL

NULL

NULL

NULL

NULL

NULL

77

&
C:
struct graphnode
{
int vertex;
struct graphnode * next ;
}
typedef struct graphnode * PTR;
main()
{
PTR graph[n];
8.3

,
,
( ) ( ).
, ,
. , , ,
,

.

.
.

(depth first search)

(breadth first search)

8.3.1

78

G
G

&
:
, .. A,
:
) .
) (push) .
) , .
A
. B. ,
.
B .
F. 1:
1

, , .
1, H.
, H
. 2:
2

1, , ,
(pop) .
, H
F. F
. B .
A . A
, C.
A, D, G I
(pop) A.
E, A.
, , A
.
3:
3

1 2,
.

79

&
A,B,F,H,C,D,G,I, E.

11
22

44

55

66

77

88

1, 2, 4, 8, 5, 7, 3, 6.
1, 3,6,8,7,5,2,4.
8.3.2

,

. , ,
.

.
. ,
:
A ,
. , :
1

( ) ,
,
(enqueue).
2

1,
, (dequeue)
( ) .

80

&
3

2, ,
.
, A
, .
A, B, C, D E. , ( front
rear) B, C, D E.
A ,
B ,
. F,
.
B
C .
D.
G, . D
E.
F, G. F H
G I.
H, I ,

. .
A, B, C, D, E, F, G, H, I.
8.4

(shortest path problem)


,
,
.
, ,
,
.

Dijkstra, Edsger Dijkstra,
1959.
81

&

( source) ( destination).

.

82

&


9.1
9.2
9.3
9.4

83

&
9.1

(hash table)
. ,
. .

. ,
, ,


, .
,
, .. . ,

,
.

.
(hashing function). ,
.
.
:

, .. 1000 .
, , 1000 bytes .
1 megabyte,
.
1 1000.
. ,
, 1000 ,
.
, ,

. ,
, .. 1001
84

&
1001 ,
.
, ,
.
, ASCII
.

.
.. 10 0 199.
10 0 9. 10,
.
, 13 13%10 = 3.

.
= %
.
.

(hash tables).
9.2 (Collisions)


. ,
13 23 3.
(collision).
(synonyms).
, ,
.
0 (open addressing).

. , ,
.
(separate chaining).

85

&
9.3 (Open Addressing)

H (linear
probing). ,
, ,
.

10 13, 28, 41, 70, 127, 131, 144, 176, 182, 199.
10 ,
:
0

70

41

131

13

144 182 176 127

28

199

131 1.
41. ,
131 , 2.
2 182,
,
. , , 182
5.
,


(quadratic probing) (double hashing).
9.4 (Separate Chaining)

,
.

(separate chaining).

86

&

33, 41, 53, 74, 87, 127, 144, 154, 178, 199
10 0 9.
10
, 74, 144, 154 .
33, 53 87, 127.
.
.
:
0

NULL

41

NULL

33

53

NULL

74

144

154

NULL

NULL

87

127

178

NULL

199

NULL

NULL

NULL

NULL

87

&


,
,

LIFO

FIFO

88

&

,
. , .

,
.

,
.

Algorithms & Data Structures


Nicklaus Wirth

Data Structures & Algorithms in JAVA


Robert Lafore

C
Leendert Ammeraal

An Introduction to the Art and Science of Programming


Walter J. Savitch

89

You might also like