data structure part 1
TRANSCRIPT
www.itdevelopteam.com 1
Array
. , . . ][L1 U1 , L2 U2 , Ln Un Array [L U] of items 1 + = U L ]1 + = [U1 L1 + 1][U2 L2 + 1][Un Ln n = (U L +1) n ) ( : ]002[ Float 0001 52 A . A[i] = (i L) n + 0011 = 0001 + 4 )0 52( = i .
2 1 3 0 2 1 5
5 6 4 2 1
2 3 Row Major 3 6 4 3 5 4
1.
Column Major 0 2 1 1 2 3 3 5 4 6 5 4
2.
A : Array [L1 U1 , L2 U2] of items ]1 + 2 = [U1 L1 + 1][U1 L = [(i L1) (U2 L2 + 1) + (j L2)] n + ] A[i , j = [(j L2) (U1 L1 + 1) + (i L1)] n + ] A[i , j
www.itdevelopteam.com 2
: , .2L1 U1 L2 U
]2 1 , 3 1[ : A
==
== C
]2[]3[A
2 5 1 6 3 4 5 = 1 + 2 2 = )1 2( + )1 + 1 2( )1 3( = ]2 , 3[ A 5 = 2 + 3 1 = )1 3( + )1 + 1 3( )1 2( = ]2 , 3[ A 1 = )1 2( + )1 + 1 2( )1 1( = ]2 , 1[ A 3 = )1 1( + )1 + 1 3( )1 2( = ]2 , 1[ A
: A [1 ... 100 , 1 ... 26] of integer 0001 ]6 , 06[ A ]4 , 02[ A .
8704 = 0001 + 2 )1 6( + )1 + 1 62( )1 06( = ]6 , 06[ A 8361 = 0001 + 2 )1 02( + )1 + 1 001( )1 4( = ]4 , 02[ A . )1 + n( n n . 2 1 6 7 )1 + 3 (3 6= = 0 2 5 2 0 0 4
0 = ]A [i , j 0 = ]A [i , j
>===== i > j >===== i < j
www.itdevelopteam.com 3
. . >===== 1 1 ( i 1) i +j 2 2 2 3 5 4 3 5 1 6 1
1 0 0 >===== 2 5 0 3 1 1
>=====
1 1
( j 1) j +i 2 2 6 3 2 4 7 5 5 6 4
1 6 7 >===== 0 2 5 0 0 4
www.itdevelopteam.com 4
, m n m n m n . .A m n + Bm n = C m n )for (i = 0 , i < m , + + i )for (j = 0 , j < n , + + j Cij = a ij + b ij
, A mL BLn .C mn = A m L B L ni k ki
3 2 1
4 5 0
1 3 2
2 1 1
43
2 1 0 1 = 1 1 3 5 23 = 24
)for (i = 0 , i < m , + + i )for (j = 0 , j < n , + + j { 0 = Cij )for (k = 0 , k < L , + + k Cij = a ik b kj + Cij }
www.itdevelopteam.com 5
: ]0, 1[ C . : for Trace. :0 = Cij Cij = a ik b kj + Cij 2 = 0 + 1 2 = Cij 2 = 2 + 0 5 = Cij 5 = 2 + 1 3 = Cij 8 = 5 + 3 1 = Cij i 1 j 0 k 0 1 2 3 L 4 Cij 0 2 2 5 8
.
2 A 1
2 5 T 3 A 7 0 5 2 0 4 2 1 1 1 2 5 0 2 3 1 3 8 4 5 8 3
1 0 7
A m n )for (i = 0 , i < m , + + i )for (j = 0 , j < n , + + j ]A [j] [i] = A [i] [j
www.itdevelopteam.com 6
Array A[n] , x Int search (A[n] , x) ; { int i = 1 ; while (i n ) return 1 // else return i // } 1 1 n 5 2 5 x 8 3 2 i 1 2 3 4 4 8 5 6 A[i] 1 5 2 8
x = A[4]
Int bsearch (A[n] , int x , int L , int U) { int i ; while {
L + U i= ; 2 if ( x < A[i] ) U = i 1 else if ( x > A[i] ) L = i + 1 else return i // i
} return 1 // }
www.itdevelopteam.com 7
x 8
i 5 2 4 3
L 1 3 4
U 01 4
]A[i 21 2 5 8
0P(x) = anxn + an1xn1 + . + a2x2 + a1x + a an 1an 2an . 1a a 2 + P(x) = 3x2 + 5x 3 + 3P(x) = 3x 0 3 3 3 0 3 5 0 5 2 3 = 5 +
2 + 001P(x) x
1 001
2 0 Stack
Stack . ) Last In First Owt (LIFO. , . top . , top top . top Stack top
. top n , top n .0 = Top Top = n === ===
push pop . ) Push (x x pop x .)x = pop pop (x
www.itdevelopteam.com 8
Stack : Array [1 .. n] of items int pop ( ) void push (int x) { { int x ; if (top = = n) if (top = 0) { { C out , = < , > , A[j { ; 1 = sw ; )]1 + swap (A[j] , A[j } ; if (sw = = 0) break } )(Insertion Sort
1 i . i . )For (i = 2 ; i < = n ; + + i { ; ]y = A[i ;1j=i ))]while (j > 0 & & (y < A[j { ; ]A[j + 1] = A[j ;1j=j } ; A[j + 1] = y }
www.itdevelopteam.com 12
05 1 05 05 04 01
06 2 06 04 05 04
04 3 04 06 06 05
02 4
01 5
03 6
i 2 3 4 5
n 6
y 06 04 02 01
06
j 1 2 1 0 3 2 1 0 4 3 2 1 0
n . : . 1 2 3 4 5 4 8 5 2 6 4 8 4 5 8 2 4 5 8 2 4 5 6 8
n 5
i 2 3 4
j 1 2 1 3 2 1 0 4 3
y 8 5 2 6
A
www.itdevelopteam.com 22
)(merge sort
n ) 2( 11 11 11 11 11 2 2 1 1 2 2 11 11 2 2 02 11 5 81 7 02 8 2 2 2 02 02 02 02 81 81 81 81 1 1 1 1 1 81 8 8 8 8 8 7 7 5 11 7 7 8 8 7 21 21 8 71 21 81 71 02 . 7 21 71 5 7 7 21 21 21 71 71 71 5 5 5 5 71
)Void mergsort (int L , int U { ; int i ) if ( L < U { ;2/)i=(L+U ; ) mergsort ( L , i ; ) mergsort ( i + 1 , U ; ) merg ( L , i , U } }
www.itdevelopteam.com 32
:
5 5 5 1 1
1 1 1 5 2
7 7 7 2 5
2 2 2 7 7 3=L 3=L 1=L 1=L 1=L 3=U 3=U 1=U 2=U 4=U 4=L 3=i 2=L 1=i 2=i 4=U 2=U
: merge sort . 1 5 2 3 3 1 4 4 4=L 3=L 3=L 4 4 5 2=L 1=L 1=L 1 3 4 1=L 4=U 4=U 4=U 2=U 1=U 2=U 4=U 1=i 2=i 3=i
A
) 4 , 1 ( Merge sort 1 3 2 5 3 1
: .
www.itdevelopteam.com 42
)(quick sort
, . , . . . ) 0(n Log n )2 0 (n . 1 21 )(Pivot 3 2 1 2 01 3 2 4 8 5 51 6 7 7 3 j 21 21 8 1 9 41 I 41 51
01 i
2 j 3
8 i 8
1 j 01
7 7
51 41
) Void quicksort (int L , int U { ; int i , j , pivot ) if ( L < U { ; ]i = L + 1 ; j = U ; pivot = A[L ) while ( i < j { ; + + while ( A [i] < pivot ) i ; - - while ( A[j] > pivot ) j ; )]if ( i < j ) swap ( A[i] , A[j } ; ) ]swap ( A[L] , A[j ; ) 1 quicksort ( L , j ; ) quicksort ( j + 1 , U } }
www.itdevelopteam.com 52
)(Link List
. )( . , . ) (node . . , . ) (Head ) (Header . 4 . 1- ) (node 2- ) (p 3- p . 4- p new node . ) Void insert ( int x , node * start { ; node * p , * q , * new node ; q = start next ; p = start ; new node = new ( node ) ; new node data = x ) 34 while ( q data < newnode 14424 datax
{ ;p=q ; q = q next } ; new node next = p next ; p next = new node }
www.itdevelopteam.com 62
: 51 :
)Start (p p Start 6 21 41
6 q q 6 21 41 02
21
41
02
03
(15) x
1- )(q2- q next p) p next ( 3- p .) void dellinklist ( int x , node * store { ; node * p , * q ; p = start ;q=p ) while ( p data ! = x { ;q=p ; p = p next } ; q next = p next ; )delete (p }
www.itdevelopteam.com 72
: 3 .5 5 8 8 3 2 \ 2 \ p 5 8 3 q 5 8 2
: ) node * f ( int x , node * start { ; node * p ; p = start ; ) while ( p ; if ( p data ! = x && p ) p = p next ; else return p } ) void g ( node * start { ) if (start ! = Null { ; C out x ) return ; ( p Left ) right = p right ; ( p right ) Left = p Left ; ) delete ( p }
. . , . , .2 1 3 5 2 0 2 + 52x + 3x
www.itdevelopteam.com 13
)(Graph
G ) = (Vertex ) , = (Edge . . , , . ) .(Adjacement . n kn . 1 ) n (n )1 n (n 2 . B
A
C
F
D E
A B A B n . n . , . , , b .
a e c = c - b - e = a - b - e - a
d
= c - a - e - b
www.itdevelopteam.com 23
: . : . n 1 - n . : . : . : . .
1-
. n n n . : )( .
d a a a b c d e f g0 1 1 0 0 0 0
b
g f
b1 0 1 1 1 0 0
c1 1 0 0 1 0 0
d0 1 0 0 1 0 0
e0 1 1 1 0 1 0
f0 0 0 0 1 0 1
g0 0 0 0 0 1 0
c
e
77 w if i , j 1 = ] A [ i , j i j A [ i , j ] = 0 if : , .
www.itdevelopteam.com 33
3 a 1 c
b 9 01
7 8
d 7 g 2
a a b c d e f g0 3 1 0 0 0 0
b0 0 9 7 8 0 0
c0 0 0 0 0 0 0
d0 0 0 0 0 0 0
e0 0 01 7 0 0 0
f0 0 0 0 5 0 2
g0 0 0 0 0 0 0
e
5
f
a d a b =A c e a b c d e0 1 1 0 0
b1 0 1 0 0
c1 1 0 1 1
d0 0 1 0 1
e0 0 1 1 0
a d a b a b c d e0 1 0 0 0
b0 0 1 0 0
c1 0 0 0 1
d0 0 1 0 0
e0 0 0 1 0
c
e
.
www.itdevelopteam.com 43
: A 2 A 3 A .
a b c d e a b c d e0 0 1 1 0 0 0 1 0 1 1 1 0 1 1 1 0 1 0 0 0 1 1 0 0
a b c d e a b c d e0 0 1 1 0 0 0 1 0 1 1 1 0 1 1 1 0 1 0 0 0 1 1 0 0
a b c d e a b 2 = A c d e1 1 1 1 2 1 1 1 2 1 1 1 4 1 1 1 2 1 1 1 2 1 1 1 1
a b c d e a b c d e1 1 1 1 2 1 1 1 2 1 1 1 4 1 1 1 2 1 1 1 2 1 1 1 1
a b c d e a b c d e0 0 1 1 0 0 0 1 0 1 1 1 0 1 1 1 0 1 0 0 0 1 1 0 03 = A
a b c d e a b c d e2 2 5 3 2 2 2 5 2 3 5 5 4 5 5 3 2 5 2 2 2 3 5 2 2
) 5 c - d ( :
c-d-c-d c-b-c-d c-e-c-d c-a-c-d c-d-e-d : 2 A . ] [ i , j Ak k i j . 2 n n . 2- . . . .
www.itdevelopteam.com 53
. n + e n + 2e. e n .
|n=|V |e=|E 1 2 3 4 5 6
2 1 2 3 3 2 2 \ \
3
5
\
3
5 6 1 \
6 1
\ \
1
4
6
5
.
)breadth first search (bfs )depth first search (dfs b a c e g d f a c e g b d
1. 2.
a-b-c-f-e-gd )(bfs
f
www.itdevelopteam.com 63
: .
, . . ) (bfs . , . ) ( . , ) bfs( . bfs . : .
b a d e
c a-b-f-c-e-d-g b a g f e d g
c
f
, . , . . , dfs .
b a d e
c a
a-b-c-d-f-e-g b
c
f
g
f e
d g
www.itdevelopteam.com 37
. e :
c a b e g d f
= e - b - c - d - f - g - a = e - c - b - a - d - f - g
Struct Linklist { int data ; struct Linklist *Next ; } typedef struct Linklist node ; struct LinkArray { int sw ; node *Link ; } typedef struct LinkArray pointer ; pointer *graphnodes ; graphnodes = New pointer [n] ; 1 2 3 4 5sw = 0
sw = 0
sw = 0
sw = 0
sw = 0
www.itdevelopteam.com 38
: . k Void bfs ( pointer graphnodes [ ] , int k ) { node *p ; graphnodes [ k ] sw = 1 ; C out