You are on page 1of 40

www.itdevelopteam.

com

Array


. , .
.
][L1 U1 , L2 U2 , Ln Un
Array [L U] of items
= U L + 1
] = [U1 L1 + 1][U2 L2 + 1][Un Ln + 1 n
= (U L +1) n ) (
: ] Float [200 1000 A25
.
A[i] = (i L) n +
= (25 0) 4 + 1000 = 1100 i
.

3 2

.1

Row Major

.2

Column Major

5
4

5
4

4
3

4
6

3
6

3
5

6
4

2
1

2
3

1
3

1
5

1
1

0
2

0
2

A : Array [L1 U1 , L2 U2] of items


] = [U1 L1 + 1][U1 L2 + 1
= [(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

: , .
L1 U1 L2 U2

] == A[3][2 C

==

]A : [1 3 , 1 2

2 5

1 6
3 4

A [3 , 2] = (3 1) (2 1 + 1) + (2 1) = 2 2 + 1 = 5

A [3 , 2] = (2 1) (3 1 + 1) + (3 1) = 1 3 + 2 = 5

A [1 , 2] = (1 1) (2 1 + 1) + (2 1) = 1

A [1 , 2] = (2 1) (3 1 + 1) + (1 1) = 3

: A [1 ... 100 , 1 ... 26] of integer 1000


] A [60 , 6 ] A [20 , 4
.

A [60 , 6] = (60 1) (26 1 + 1) + (6 1) 2 + 1000 = 4078


A [20 , 4] = (4 1) (100 1 + 1) + (20 1) 2 + 1000 = 1638


.
)n( n + 1
n .
2
1 6 7

)3( 3 + 1

=6
=
0 2 5
2
0 0 4

>===== i > j

A [i , j] = 0

>===== i < j

A [i , j] = 0

www.itdevelopteam.com
3



.
.
( i 1) i
+j
2

6
1

5
1

4
3

6
4

5
5

4
7

3
5

2
2

( j 1) j
+i
2
3
2

2
6

1
1

1
1

>=====

1 0 0

2
5
0
>=====

3 1 1

>=====

1 6 7

>===== 0 2 5
0 0 4

www.itdevelopteam.com

, 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 n
ki

2
1

0
1

1
1

42 = 32

1
1

i k

2
1

34

)for (i = 0 , i < m , + + i
)for (j = 0 , j < n , + + j
{
Cij = 0
)for (k = 0 , k < L , + + k
Cij = a ik b kj + Cij
}

www.itdevelopteam.com
5

: ] C [1 ,0 .
: for Trace .
:
Cij = 0
Cij = a ik b kj + Cij
Cij = 2 1 + 0 = 2
Cij = 5 0 + 2 = 2
Cij = 3 1 + 2 = 5
Cij = 1 3 + 5 = 8
Cij

.
1

0
7

2
5

A T 3

7
0

2 0 4 2 1 1

1 2 5 0 2 3
1 3 8 4 5 8

2
A
1

A m n
)for (i = 0 , i < m , + + i
)for (j = 0 , j < n , + + j
]A [j] [i] = A [i] [j

www.itdevelopteam.com

Array A[n] , x
Int search (A[n] , x) ;
{
int i = 1 ;
while (i <= n && A[i] != x)
i++;
if (i > n ) return 1 //
else return i //
}
1
1

2
5

n
5

3
2
x
8

4
8
i
1
2
3
4

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

]A[i
12
2
5
8

L
1
3
4

U
10
4

i
5
2
4
3

x
8

P(x) = anxn + an1xn1 + . + a2x2 + a1x + a0


an
an1
an2
.
a1
a
+

P(x) = 3x2 + 5x + 2
P(x) = 3x3 + 3

=
5

100

P(x) x100 + 2

Stack

Stack
. ) Last In First Owt (LIFO .
, . top .
, top top .
top Stack top

. top n top ,
n .

===

Top = 0

===

Top = n

push pop Push (x) . x


pop x .
)x = pop pop (x

www.itdevelopteam.com

Stack : Array [1 .. n] of items


int pop ( )
void push (int x)
{
{
int x ;
if (top = = n)
if (top = 0)
{
{
C out << ;
return 1 ;
C out << ;
return 1 ;
}
}
else
else
{
{
top + + ;
x = Stack [top] ;
Stack [top] ;
top = top 1 ;
}
}
}
return x ;
}
C B A :
n=5

A = 10

B=2

C=5

push (B)
push (A + B)
pop (C)
push (A B)
push (C)
push (B)
pop (A)
pop (B)
push (A B)
push (C)
push (A)
pop (B)
pop (C)
pop (A)

2
2
12
12
2

12
24
8

10

12

12

24

12

www.itdevelopteam.com
9

: top1
top2 top1 . top2
. top1 = 0 top2 = n + 1.
1 ===

top1 = 0

2 ===

top2 = n + 1

===

top2 = top1 + 1
1

top2 = n + 1

top1



. 1 , 2 , 3 , 4 . 2
push push 1 3 push
1 2 push .
: 1 , 2 , 3 , 4 .
4 3 1 2

4 2 3 1

3 2 4 1

3 1 4 2

2 1 3 4

push 1
push 2
push 3
push 4
pop 4
pop 3

push 1
push 2
push 3
push 4
pop 4

push 1
push 2
push 3
pop 3
pop 2
push 4
pop 4
pop 1

push 1
push 2
push 3
pop 3

push 1
push 2
pop 2
pop 1
push 3
pop 3
push 4
pop 4

) (1 2 3 4 a b c
b < c < a abc .

www.itdevelopteam.com

10

a b + c / d :
)(
) ,( Not ,

1.
2.

and , , / , mod

3.

OR , + ,

4.

5.
)= !( > < < , > , < = , > = ,
: .

a+b

infix

ab +

postfix

+ ab

prefix

-1
-2 , .
-3 , .
-4 .
:

)) ((a + (b (c a ))) (b / c
1

2
3
5
postfix = (a (b( ca ) ) + (bc ) / ) = abca + bc /
prefix = +a b ca / bc

www.itdevelopteam.com
11

infix postfix

-1 infix .
-2 push.
-3 .
-4 top
push top pop
.
-5 pop .
:

)) ((a + (b (c a ))) (b / c

abca + bc /
/
(

(
+
(
(

: , postfix.

a + bc a b/c

abca + bc /

www.itdevelopteam.com

12

: , postfix.

a + ( b c) a b / c

abc a + bc /

infix prefix .

push . pop infix postfix


. push . pop
, pop prefix
push . infix postfix.
: infix prefix.

a + ( b c) d / a c b
d
/ bcda

d
a
bc bcd
+ a / bcda

c
b
a

(
+

+a / bcda cd
: infix prefix .
) a + b c ( 2 b ) c / (d + a

+da

d
/ b c-2bc+da

a
c
b c-2b

-2b
c-2b
b c-2b

+a/ b c-2bc+da

b
2
c
b
a

+
(
/

postfix infix

Stack postfix infix .

www.itdevelopteam.com
13

postfix . push .
, pop infix . infix
push . ,
infix . infix . top
.
:

abca +bc/

b/c

c
b
))(a+b (c a))(b/c

c a
)b (c a
))a+(b (c a

a
c
b
a

prefix infix

prefix infix .
push , pop
infix push . top
.
+a b ca/bc

))(a+(b (c a

))(b (c a

a
)(c a
)(a+b (c a) (b/c

b
a
)(b/c

c
b
c

postfix prefix

postfix prefix infix


infix .
postfix prefix .
,
pop infix push
postfix prefix push.

www.itdevelopteam.com

14

)(queue


. (First In First Out) FIFO
. , .
front rear .
.
n front rear n
front rear .

front = rear = 0

front rear rear n


.

===

rear = n

front = rear ===

,
delqueue Addqueue . ) Addqueue(x x
delqueue x
.

x = delqueue

Addqueue delqueue

queue : Array [1 .. n] of item




)void Addqueue (int x
) ( int delqueue
{
{
)if (rear = = n
)if (front = = rear
; << C out
{
else
; << C out
{
; return 0
; rear + +
}
; queue[rear] = x
else
}
{
}
; front + +
; ]x = queue[front
; return x
}
}

www.itdevelopteam.com
15

: A B C .
n=4
13
4

C=2
2
2

20
3

B = 10

A=5
)Addqueue (A + B

15
1

)Addqueue (C
)Addqueue (B C

C
2

B
10
2

A
5
15

Rear Front
0
0
1
1
2
2
3
4

) ( A = delqueue
) ( B = delqueue
)Addqueue (B A

C=2

B=2

A = 15

Addqueue delqueue .

.
)( rear front
. n n 1
.
queue : Array [0 .. n 1] of item
rear = n 1 ] queue[0 .
front = rear
.

front = (rear + 1) mod n ===


===

front = rear

rear , rear = n 1
. rear .
rear = (rear + 1) mod n
front .
front = (front + 1) mod n

www.itdevelopteam.com

16

void Addqueue (int x)

int delqueue ( )

{
rear = (rear + 1) mod n

if (front = = rear)

if (front = = rear)
C out << ;

{
C out << ;

else

return 0 ;
queue[rear] = x ;

else
{
front = (front + 1) mod n
x = queue[front] ;
}
}

Addqueue [50]
Addqueue [20]
Addqueue [30]
delqueue ( )
Addqueue [10]

r=1

f=0
r=2

f=0
r=3

f=0
r=3

f=1
r=0
f=1

0
10

1
50
1
50
1
50
1
50
1
50

2
20
2
20
2
20
2
20

3
3
30
3
30
3
30

www.itdevelopteam.com
17

: prefix postfix.

) a 2 bc (a 2 b c ) (1 / 2
postfix = a 2 bc 12 /
prefix = a 2 bc / 12


.
)(Selection Sort

n )] n 1 , (A[1..n .
.
.
, .
:

12

25

20

10

25

12

20

10

25

20

12

10

25

20

12

10

25

20

12

10

25

20

12

10

www.itdevelopteam.com

18

:
)for (i = n ; i > 1 ; 1
{
; ]max = A[1
; index = 1
)for (i = 2 ; j < = i ; + + j
)if (A[j] > max
{
; ]max = A[j
; index = j
}
; ]A[index] = A[i
; A[i] = max
}
:

index

max

4
2
3

: , n2 .
n .

www.itdevelopteam.com
19

)(Bubble Sort

n )] n 1 , (A[1..n
. ,
.
20




90

40

50

30

10

20
90
90

90
20
20
50
50

30

10

30

10

90

50

40
50
50
20
20
40
40

50
40
40

30

10

90

50

40

30
20
20

10

90

40
20
20
30
30

10

)for (i = 1 ; i < n ; + + i
)for (j = 1 ; j < = n ; + + j
)]if (A[j]) > A[j + 1
; )]swap (A[j] , A[j + 1

)for (i = 1 ; i < n ; + + i
)for (j = n ; j > = i ; j
)]if (A[j]) < A[j 1
; )]swap (A[j] , A[j 1
:

www.itdevelopteam.com

20

n2 . .

)for (i = 1 ; i < = x ; + + i
{
; sw = 0
)for (j = 1 ; j < n 1 ; + + j
)]if (A[j] > A[j + 1
{
; sw = 1
; )]swap (A[j] , A[j + 1
}
; if (sw = = 0) break
}
)(Insertion Sort

i 1 . i
.
)For (i = 2 ; i < = n ; + + i
{
; ]y = A[i
;j=i1
))]while (j > 0 & & (y < A[j
{
; ]A[j + 1] = A[j
;j=j1
}
; A[j + 1] = y
}

www.itdevelopteam.com

j
1
2
1
0
3
2
1
0
4
3
2
1
0

y
60
40
20
10

21

n
6

30
6

i
2
3
4
5

10
5

20
4

60

40
3

60
2

50
1

40

60

50

60

40

50

60

50

40

50

40

10

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

y
8
5
2
6

j
1
2
1
3
2
1
0
4
3

i
2
3
4

n
5

www.itdevelopteam.com

22

)(merge sort

n
) (2


.
7
12
17
5

18

20

11

12

18

20

11

17

12

18

20

17

12

18

20

17

18

17

11

2
2
2

11
11

11

20

11

17

12

20

18

11

20

18

17

12

11

12

)Void mergsort (int L , int U


{
; int i
) if ( L < U
{
;i=(L+U)/2
; ) mergsort ( L , i
; ) mergsort ( i + 1 , U
; ) merg ( L , i , U
}
}

www.itdevelopteam.com
23

2
U=4
U=2

L=4
i=3
L=2
i=1
i=2

U=3
U=3
U=1
U=2
U=4

L=3
L=3
L=1
L=1
L=1

: merge sort .
U=4

L=4

U=4

L=3

U=4

L=3

U=2

L=2

U=1

L=1

i=1

U=2

L=1

i=2

U=4

L=1

i=3

4
4

3
1

2
3

1
5

) Merge sort ( 1 , 4
4
4
5

3
1
4

: .

2
5
3

1
3
1

www.itdevelopteam.com

24

)(quick sort

,
. ,
.

.
. ) 0(n Log n
) 0 (n2 .
9
14
I

8
1

7
3
j

5
15

6
7

14

15

12

1
j

15

14

12

10

4
8

8
i
8

3
2

2
j
3

2
10

1
12

)(Pivot

10
i

3
1

) 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
; ) quicksort ( L , j 1
; ) quicksort ( j + 1 , U
}
}

www.itdevelopteam.com
25

)(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
while ( q data < newnode
data
1442
44
)3
x

{
;p=q
; q = q next
}
; new node next = p next
; p next = new node
}

www.itdevelopteam.com

26

: 15 :

30

20

14

12

)Start (p

(15) x
p
Start
6
12
14

q
6
12
14
20

-1 )(q
-2 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
27

: 3 .
q
5
8
2

p
5
8
3

:
) 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 << start data
; ) g ( start next
}
}

: f g
.

www.itdevelopteam.com
28


Null ) (start
.
: .

www.itdevelopteam.com
29

.
data -1

. -2
. -3
struct Linklist
{
struct Linklist * left ;
data ;
struct Linklist * right ;
}
typedeg struct Linklist node
node * p , * q ;

(new node) -1

-2
new node p -3
void insert (int x , node * start )
{
node * newnode , * p ;
newnode = new (node) ;
1 newnode data = x ;
newnode right = Null ;
newnode left = Null ;
p = start right ;
2 while ( p data < x ) p = p right ;
p = p left ;
( p right ) left = newnode ;
3 newnode left = p ;
newnode right = p right ;
p right = newnode ;
}

www.itdevelopteam.com

30

: 6 .
\

start

) void delete ( int x , node * start


{
; node *p
; p = start right
; while ( p && p data < x ) p = p right
; if ( p = = Null | | p data > x ) return
; ( p Left ) right = p right
; ( p right ) Left = p Left
; ) delete ( p
}


. .
, .

,
.
0

2x + 3x5 + 2

www.itdevelopteam.com
31

)(Graph

G ) = (Vertex ) , = (Edge
. .
, ,
.
) .(Adjacement
. n kn .
1
) n (n ) n (n 1
2
.
B

D
E

A B A B
n n . .
,
. , ,
b
.

a
e
d

c
= c - b - e

= c - a - e - b
= a - b - e - a

www.itdevelopteam.com

32

: .
: .
n n - 1 .
: .
: .
:
.

.

-1

. n n
n .
: )( .

d
b

f
e

a
b
c
d
e
f
g

77
w if i , j A [ i , j ] = 1
i j A [ i , j ] = 0 if
: , .

www.itdevelopteam.com
33

10

a
b
c
d
e

a
b
c
d
e

a
b
c
d
e
f
g

d
g

3
a

2
f

10

d
b

=A
e

d
b

www.itdevelopteam.com

34

: A A2 A3 .

a b c d e
2 1 1 1 1
1 2 1 1 1
1 1 4 1 1
1 1 1 2 1
1 1 1 1 2

a b c d e
a
b
2
= A
c
d
e

a b c d e
2 3 5 2 2
5 5 4 5 5

2 2 5 2 3

d
e

2 2 5 3 2

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

a
b

c
d
e

a b c d e
a
b

3 2 5 2 2

0 1 1 0 0

a b c d e

3
= A

0 1 1 0 0
1 1 0 1 1

0 0 1 0 1

d
e

0 0 1 1 0

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

a b c d e
a
b

1 0 1 0 0

0 1 1 0 0

a
b
c
d
e

2 1 1 1 1

a
b

1 1 4 1 1

1 1 1 2 1

d
e

1 2 1 1 1

1 1 1 1 2


) 5 c - d ( :

c-d-c-d
c-b-c-d
c-e-c-d
c-a-c-d
c-d-e-d
: A2 .
] [ i , j Ak k i j .
n2 n
.
-2
.
. .
.

www.itdevelopteam.com
35


.
n + e
n + 2e e . n .

|n=|V
|e=|E
\

4
5
6

.
.1

)breadth first search (bfs

.2

)depth first search (dfs


b

a-b-c-f-e-gd

b
c

)(bfs

e
g

f
g

www.itdevelopteam.com

36

: .

, .
.
) (bfs .
,
. ) ( .
, ) bfs( .
bfs .
: .

a-b-f-c-e-d-g
b
a

,
.
, .
. , dfs .

a-b-c-d-f-e-g
b

a
d
d

f
e

f
e

www.itdevelopteam.com
37

. e :

c
a

= e - b - c - d - f - g - a

= e - c - b - a - d - f - g

g
d

Struct Linklist
{
int data ;
struct Linklist *Next ;
}
typedef struct Linklist node ;

1
2
3

struct LinkArray

5
int sw ;
node *Link ;

}
typedef struct LinkArray pointer ;
pointer *graphnodes ;
graphnodes = New pointer [n] ;

sw = 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 << k ;
Addqueue ( k ) ;
. k
while ( ! ( queue . empty ( )))
{
k = delqueue ( ) ; . k
p = graphnodes [ k ] . Link ;
do
{
if ( graphnodes [ p data ] . sw = = 0 )
{
addqueue ( p data ) ; C out << p data ;
graphnodes [ p data ] . sw = 1 ;
}
p = p Next ;
}
while ( p ) ;
}
}

Graphnodes [ 5 ]

1
sw = 0

sw = 0

sw = 0

sw = 0

sw = 0

1
5
2
P

2
P

5
P

1
P

4
P

www.itdevelopteam.com
39

k
1

1
3

5
2
4

: . k

Void dfs ( pointer graphnodes [ ] , int k ) //


{
node *p ;
graphnodes [ k ] . sw = 1 ; C out << k ;
for ( p = graphnodes [ k ] . Link ; p ! = Null ; p = Next )
if ( graphnodes [ p data ] . sw = = 0 )
dfs ( graphnodes , p data ) ;
}
:

1
sw = 0

sw = 0

sw = 0

sw = 0

sw = 0

1 3 2 5 4

1
5
2
P (6)

P (5)

2
P (3)

P (9)

P (2)

5
P (12)

P (4)

P (1)

P (11)

P (10)

1
P (7)

4
P (8)

www.itdevelopteam.com

40

P (3,1) = Null

)P (2,1

)P (1,1

\
P (2,2) = Null

)P (1,2

.
k
1
2
3
4

1 2 3 4

)P (1,4

= sw

3
1

3
P (2,4) = Null

)P (1,3

= sw

2
1

)P (2,3

P (3,3) = Null

= sw

4
1

= sw

) (

)(
.
) : (kraskal ,
. )(
.
.

a
8
3

f 5
2

b
5

10
6

9 bf = 3

f 5
c

9 fe = 2

20
5

7
e

d
2 + 3 + 5 + 5 + 7 = 22

c
7

9 bd = 5 , 9 ae = 5
8 be = 6
9 cd = 7
8 de = 8 , af = 8
8 df = 9
8 ab = 10
8 bc = 20

You might also like