Anda di halaman 1dari 95

01 - Introduction

:
.
.



o
o
o ,

:
, - .
:

:
a 10
"="- . Java -
, field (Obj ) : ). key(node
. Obj. field
Java -":
:
for i 1 to n
for i 100 downto 1
)(:
)( .
If grade = 100
print Thats an A
else
print Not an A
print Just + grade
print Thats it

} { - , .
-" .
,- .
Bubble Sort
5, 2, 4, 6, 1, 3

Input:

Output: 1, 2, 3, 4, 5, 6

)BubbleSort (A
)1. n length(A
2. for j n-1 to 0
3.
for i 0 to j-1
4.
]if A[i] > A[i+1
5.
]tmp A[i
6.
]A[i] A[i+1
7.
A[i+1] tmp
:
524613
245136
241356
213456
123456
123456

:
" . c -
" ".
: BubbleSort
)n(n 1
)n(n 1
+4
] = 2.5n 2 c 1.5nc + c
2
2

n 1

n 1

k =1

k =1

T(n ) c [1 + (n 1) + k + 4 k ] = c [1 + (n 1) +

line 1 line 2 line 3 lines 4 7

, ,
.
:

)T (n ) = O(...

:
n -
.
, , .

:

)BubbleSortCheck (A
j length(A) - 1
ordered false
)while ( j > 0 && !ordered
ordered true
for i 0 to j -1
]if A[i] > A[i+1
]tmp A[i
]A[i] A[i+1
A[i+1] tmp
ordered false
jj-1
:
6 3 4 7 5
3 4 6 5 7
3 4 5 6 7

:
) ( Worst Case
) T (n ) = O(... .
) ( Average Case
) T (n .
) ( Best Case
) T (n .
T (n ) = (...) :
: , .
)O(...) = (...
T (n ) = (...) :
:
: BubbleSortCheck
, ) , O(n .
2
) ( , ) O(n.

02-Asymptotic Notation
O :
, .
:
.
.
, .
, .
: .
:
) ( .
n , .
:
. n . T (n ) -
:
, .
, n n
)( , .
:

11
T (n ) c n 2 + 10cn + 100c
2
) n 2(.
T (n ) c n 3 + 1000000n + 1
. n 3
T (n ) 100cn + 1000c
.
T (n ) 1000c
). (1

n , n,
.

:
) T (n " ) g (n n 0 c :
) n > n0 : T (n ) < c g (n

, n , ) ( , ) T (n
g (n ) - . n
:
) T (n " ) , g (nT (n ) = O( g (n )) :
:
)4n + 100 = O(n
4n + 100 Cn
4 + 100/n C
n0 = 100, C=5; n0=1, C=104
:
)) T (n ) O( g (n n0 c R -n > n0 : T (n ) > c g (n ) :
+

:
, ?
c = 1, n0 = 4 ,
c = 2, n0 = 3 ,

c = 1, n0 = 1 ,

)3n + 1 = O(n2
)(n + 1)2 = O(n2
2

)(n + 1) = O(n
)8 = O(1
)log(n) = O(n
)nlog(n) = O(n

:
) T (n " ) g (n n0 c :
) n > n0 : T (n ) > c g (n

, n , ) ( , ) T (n
g (n ) - . n

:
) T (n " ) , g (n:

)) T (n ) = ( g (n

)4n + 100 = (n
4n + 100 Cn
n0=1, C=4

:
)) T (n ) ( g (n n0 c R -n > n0 : T (n ) < c g (n ) :
+

:
, ?

c = 1, n0 = 1 ,
c = 1 , n0 = 4 ,
2


c = 1, n0 = 1 ,

)3n + 1 = (n2
)(n + 1)2 = (n
)(n - 1)2 = (n2
)8 = (1
)log(n) = (n
)nlog(n) = (n

:
) T (n " ) g (n n 0 c2 , c1 :
) n > n0 : c1 g (n ) < T (n ) < c 2 g (n

, n , ) ( , ) T (n
" ) g (n . n
:
) T (n " ) , g (nT (n ) = (g (n )) :
:

)4n + 100 = (n
C1 n 4n + 100 C2n
C2 4+100/n
C1 4+100/n
n0=100, C2=5, C1=4

:
)) T (n ) (g (n n0 c1 , c 2 R -:
)) n > n0 : (c1 g (n ) > T (n )) (T (n ) > c 2 g (n
+

:
, ?

)3n + 1 = (n2
)(n + 1)2 = (n2
)(n + 1)2 = (n
)8 = (1
)log(n) = (n
)nlog(n) = (n

: )(: (), (), O

:
)) f (n ) = O(g (n )) f (n ) = ( g (n )) f (n ) = (g (n
:
" } , {O, , :
)) f (n ) = ( g (n )) g (n ) = (h(n )) . f (n ) = (h(n
:
log n = O n c , n = O e log n = O e
:
" } , {O, , :
)) . f (n ) = ( f (n
:
)) f (n ) = ( g (n )) . g (n ) = ( f (n

) (

) (
n

:
(n + 1) = (n 1) : (n 1) = (n + 1) :
:
)) f (n ) = O( g (n" )) . g (n ) = ( f (n

) (
n

:
n =O n
: a, b 0
a +b
n a = O n a +b ," n = n a

) (

a +b

1
:
)T(n) = 1 + 2 + 3 + 4 + 9 + 8 + 27 + . + 2n + 3n = (3n

T(n) = 1 + 2 + 4 + 8 + + 2n + 3 + 9 + 27 + + 3n =
n+1
=

2 n +1 1
3n 1
3
3
5
+3
= 2 n +1 1 + (3 n 1) = 2 * 2 n + 3 n
2 1
3 1
2
2
2

T(n) 2 * 3n + 3/2 * 3n = 7/2 * 3n = C1 3n


n

: C1 > 0 n0

T(n) = 2 * 2n + 3/2 * 3n 5/2 C1 3n

C1 = 7/2, n0 = 1

: C2 > 0 n0

T(n) = 2 * 2 + 3/2 * 3 5/2 C2 3

T(n) = 3/2 * 3n + (2 * 2n - 5/2) 3/2 * 3n = C2 3n

C2 = 3/2, n0 = 1

T(n) = (3n)

2
:
log(n!) = (n logn)
:
log(a*b)=loga + logb
log(n!) = log(n*(n-1)!) = logn + log[(n-1)!] = ... = log1 + log2 + + log(n-1) + logn

log1 + ... + log(n-1) + logn < logn + log n + ... + logn = n logn = O(n logn)
n

log1 + log2 + ... + log(n-1) + logn > log(n/2) + + log(n-1) + logn >
> log(n/2) + log(n/2) + + log(n/2) = n/2 log(n/2) = n/2(logn log2)
= (n logn)

C1 = 1, n0 = 1

n/2
C2 = 5, n0 = 1

log(n!)= (nlogn)
3

for (i = 1; i < n; i++)


for (j = 0; j < n; j += i)

s++
for 1: n times
for 2:
i=1
i=2
i=3

for 2: n times
for 2: n/2 times
for 2: n/3 times

T(n) = n + 2(n + n/2 + n/3 + + n/n) = n + 2n(1 + + 1/3 + 1/n) =


= n + 2n(1 + ln n) = O(n logn)

03 - Divide & Conquer


) (.

.
)(.

Merge Sort
MergeSort .
) ,(1 ) (.
) (.

.
)MergeSort (A
)n length(A
if n > 1
mid n/2
]A1 A [0 mid-1
]A2 A [mid n-1
)MergeSort(A1
)MergeSort(A2
)Merge(A1, A2, A
:
5, 6, 7, 10, 11

6, 7, 11

6, 11

11

7, 6, 11

5, 10

Merging:

10, 5, 7, 6, 11

10

6, 11

11

10, 5

Splitting:

10

. A A2 - , A1 Merge()
? Merge()
Merge(A1, A2, A)
ind1 = 0
ind2 = 0
for i 0 to i < length(A) ; i++
if ind2 = length(A2)
A[i] A1[ind1++]
else if ind1 = length(A1)
A[i] A2[ind2++]
else
if A1[ind1] < A2[ind2]
A[i] A1[ind1++]
else
A[i] A2[ind2++]
O(n = n1 + n2 ) Merge()

: MergeSort
MergeSort(A)
n length(A)
if n > 1
mid n/2
A1 A [0 mid-1]
A2 A [mid n-1]
MergeSort(A1)
MergeSort(A2)
Merge(A1, A2, A)

1
1
1
n/2
n/2
T(n/2)
T(n/2)
n
:

(1)
n =1

T (n ) =
2T (n / 2) + (n ) n > 1

Binary Search -
: A value
int BinarySearch( A, value)
return BinarySearch( A, value, 0, |A|-1 )
int BinarySearch( A, value, first, last )
if first > last
return -1
m (first + last) / 2
if A[m] = value
return m
else if value < A[m]
BinarySearch( A, value, first, m-1 )
else
BinarySearch( A, value, m+1, last )

1
1
1
1
1
1
T (n / 2 )
or
T (n / 2 )

:
(1)
n =1

T (n ) =
T (n / 2 ) + (1) n > 1

04 - Recurrences

: /
'' .
':
n =1

= )T ( n
2T n + c n > 1
2
. n
:
,
Substitution method -
Iteration method -
Master method -

: :
)int BinarySearch(key, A
)BinarySearch(key, A, 0, length(A)-1
)int BinarySearch(key, A, first, last
)if (first > last
return 1
mid ( first + last ) / 2
)if (A[mid] == key
return mid
)else if (A[mid] > key
)BinarySearch(key, A, first, mid-1
else
)BinarySearch(key, A, mid+1, last

:
T (1) = 1
T (n ) = T (n / 2) + 1

)( Substitution method

, ) (
,

:
T (1) = 1
T (n ) = T (n / 2) + 1

T (n ) = O(log n ) :
: T (n ) c log n :
.1 T (1) = 1 c log(1) = 0
, n > n0 n0.
, T (2) = T (1) + 1 = 2 c log 2 = c : . c 2
.2 , m , 2 m < n . n
T (n ) = T (n / 2) + 1 c log(n / 2) + 1 = c log n c log 2 + 1 = c log n (c 1) c log n .3
.4 c 2 n > n0 . n0 = 2
MergeSort :
T(1)=1
T(n) = 2T(n/2) + n
T(n) = O(n logn).
need to prove that T(n) c n logn

Guess:
Proof:

)1. Try T(1) = 1 c *1* log(1) = 0 (wrong, but not a problem


Try T(2) = 2T(1) + 2 = 4 c*2*log(2) = 2c, let c 2
2. Assume that for any m, such that 2 m < n the guess is right
3. Check that for n,
)T(n) c n log(n
= T(n) = 2T(n/2) + n 2(c*n/2*log(n/2)) + n = c n log(n/2) + n
)= c n log(n) c n log(2) + n = c n log(n) - (c - 1) n < c n log (n
:
n , .
, . T (constant) = (1) :

Iteration method
.
.
.

:1
T(0) = 0
T(n) = T(n-1) + n
T(n)

= T(n-1) + n
= T(n-2) + (n -1) + n
= T(n-3) + (n 2) + (n -1) + n
=
= T(n-k) + n-(k-1) + + (n-3) + (n-2) + (n-1) + n
?
. n = k , n k = 0

T(n)

= T(0) +

i =1

i =1

i = 0 + i = n

n +1
= n2
2

( )
:2
T(1) = c
T(n) = 2 T(n/2) + n

T(n) = 2 T(n/2) + n = 2(2 T(n/4) + n/2) + n


= 4 T(n/4) + n + n = 4(2T(n/8) + n/4) + 2n
= 8 T(n/8) + 3n =
= 2k T(n/2k) + kn
log n

T(n) = 2

log n

T(n/2

: k = log n

) + log n * n = n T(1) + n logn = (n logn)


:3
T(n) = 3 T(n/4) + n

T(n) = 3 T(n/4) + n = 3(3 T(n/16) + n/4) + n


= 9 T(n/16) + 3/4n + n = 9 (3T(n/64) + n/16) + 3/4n + n
= 27T(n/64) + 9/16n + 3/4n + n =
n k 1 3
= 3k T k + ( ) i n
4 i =o 4
k = log 4n :
k 1

3
3
1
= O(n)
T(n) = 3 log 4 n T (1) + n ( ) i < n log 4 3T (1) + n ( ) i = n log 4 3 + n
1 3/ 4
i =o 4
i =o 4

1
a logb n = n logb a , k i =
for k < 1 :
1 k
i =0

Master Theorem

:
n
. b > 1 - a 1 , T (n ) = aT + f (n )
b
: master

n logb a

T (n) = n logb a log n

( f (n) )

> 0 : f (n) = O n logb a

f (n) = n logb a

)
(

> 0 : f (n) = n logb a+


c < 1, n0n > n0 : af (n / b) < cf (n)

:1
T(n) = 9T(n/3) + n

a = 9, b = 3, f(n) = n
Since f(n) = n = O( n log 3 ( 9 ) ), where =1,
case 1 applies:

T(n) = n log b a = n log 3 9 = (n2)

( ) :2
T(n) = T(n/2) + 1
a = 1, b = 2, f(n) = 1
Since f(n) = 1 = ( n log b a = n log 2 1 = n0 = 1),
case 2 applies:

T(n) = ( nlogb a log n) = ( log n)

:3
T(n) = 3T(n/4) + n logn
a = 3, b = 4, f(n) = n logn
nlogb a = nlog4 3 = O(n0.79)
n log n (n 0.79...+ )

: c = 3/4 - n > 1 :

3(n / 4) log(n / 4) 3 n log n


4
Thus: case 3 applies:

T(n) = (n logn)

:4
T(n) = 2T(n/2) + n logn

a = 2, b = 2, f(n) = n logn
n log b a = n log 2 2 = n

( )

f(n) = n log n = n1+ > 0 , f(n) = n log n = (n ) -


log n < n : > 0

. master ,

05 - Sorting Bounds

.
. BubbleSort, InsertionSort, MergeSort :

, n } . {x1 ..x n
? .
, , .
, .
, .
, : , - .
)( .
: :

,
.
.
, , .
! n
h = log(n!) = (n log n ) .
) . (n log n

: :
, ) . (n log n

Counting Sort
[0, k]

k+1

)Counting-Sort(A, B, k
for i 0 to k
do C[i] 0
for j 0 to length(A)-1
do C[A[j]] C[A[j]] + 1
for i 1 to k
]do C[i] C[i] + C[i-1
for j length[A]-1 downto 0
]do B[C[A[j]]-1] A[j
C[A[j]] C[A[j]] 1

)O(k
)O(n
)O(k
)O(n

) , O(n+k ) k = O(n )O(n


:

. .
:

Radix Sort
Counting Sort - ) .( Radix-Sort
:
n ) (0, n ) , m=n ,
) . O(n+m)=O(n :
2

.1
.2

, Counting Sort - }{ai mod n


" }{ai / n

Counting Sort - , ,
, .


:
36, 9, 0, 25, 1, 49, 64, 16, 81, 4
n ,10 ) .(0,99 ) ( :
0, 1, 81, 64, 4, 25, 36, 16, 9, 49
) ( :
0, 1, 4, 9, 16, 25, 36, 49, 64, 81
:
)RadixSort(A, d
for i= 1 to d
use Counting-Sort to sort A on digit i
:

)O(d(n+k)) = O(dn+dk

-
. ,
, , :
{public class Date
;int day
;int month
;int year
}
day month , year
) ,(1900<< year =2000 : ,
. " " )"" -
( , .
:


) O(n

9
9

Insertion
Bubble
Merge

)O(nlogn

9 merge-

Counting

)O(n+k

) O(n

9
9

06 Simple Data Structures



:
) . (
:
) ( .
List } {x1 ...x n ) (
List:
) get( i i - .
) put( i, value i - .value
) - add( value value .
) remove( i i -.
List .
) get( index
) put( index, value
|if index 0 and index < |L
|if index 0 and index < |L
]return L[index
L[index] value
else
return ERROR
O(1) :
O(1) :
) add( value
) remove( index
|n |L
|n |L
]L1 new array[n+1
) L1 new array( n - 1
for i 0 to n1
for i 0 to n 2
]L1[i] L[i
if i < index
L[n] value
]L1[i]L[i
L L1
else
]L1[i]L[i + 1
LL1
O(n ) :
O(n ) :
:
data - .
:
data -
.

:( Java - ): LinkedList
Linked List
class Item{
public Item( Object d, Item n ){
data = d;
next = n;
}
public Object data;
public Item next;
}
class LinkedList{
private Item head;
private Item tail;
public LinkedList(){
head = null;
tail = null;
}
public void addTail( Object data ){
Item newTail = new Item( data, null );
if( head == null ){
head = newTail;
tail = head;
}
else{
tail.next = newTail;
tail = newTail;
}
}
public void addHead( Object data ){
Item newHead = new Item( data, head );
if( head != null )
head = newHead;
else{
head = newHead;
tail = head;
}
}

public Object removeHead(){


if( head == null )
return null;
Object data = head.data;
head = head.next;
if( head==null )
tail= null;
return data;
}
public int find( Object data ){
if( head == null )
return -1;
int index = 0;
Item current = head;
while((current != null) && ( !current.data.equals( data ) ) ){
index++;
current = current.next;
}
if( current == null )
return -1;
else
return index;
}
public Item get( int index ) {
Item p = head;
for( int i = 0; i < index; i++ )
if( p != null )
p= p.next;
return p;
}
public void put( int index, Object data ) {
Item p = head;
for( int i=0; i<index; i++ )
if( p != null )
p= p.next;
if( p!=null )
p.data= data;
}
public int count()
{
int c= 0 ;
for( Item p= head ; p!=null ; p=p.next )
c++;
return c;
}

public void remove( int index )


{
if( index<0 || index>=size ) // assuming the member size exists
return ERROR_OUT_OF_BOUND
if( index==0 ) // one less special case to handle
return removeHead() ;
Item p = head;
for( int i=1; i<index-1; i++ )
if( p != null )
p= p.next;
if( p==null )
return ; // ERROR
if( tail==p.next )
tail= p ;
p.next= p.next.next ;
// that should do it
}
}
: LinkedList List
get(index)
L.get(index)

add (data)
L.addTail(data)

put(index,data)
L.put(index,data)

remove(index)
L.remove (index) ;

:
Operation

Using Array

get
put
add
remove

O(1)
O(1)
O(n )
O(n )

Simple List
using LinkedList
O(n)
O(n)
O(1)
O(n)

Stack
, .
( LIFO: last in, first out ) .

)push(x
top() value
pop() value
) (MakeEmptyStack
)(IsEmptyStack

:
) Program-Counter (PC , .
PC .
:
. CPU-
PC- .
. CPU -
:
PC -
. PC -
:
. PC -

:
)g(v
;v=3
;w=5
;return w

)f(x
;a=2
;)b=g(a
;c=4

main
;x=5
;)f(x
;z=8

:
))push(PC f(x
))push(PC b=g(a
PC pop() /* b=g(a) */
PC pop() /* f(x) */
!*/ ... *PC pop() /

:
- A , .
last . . 1 -

)O(1

) push(value
last++
A[last] = value

)O(1

)(pop
if last < 0
return ERROR
last-]return A[last+1

)O(1

)(top
if last<0
return ERROR
]return A[last

: LinkedList
)O(1

) push(value
) L.addHead( value

)O(1

)(pop
)(return L.removeHead

)O(1

)(top
) return L.get( 0

: top
)(top
)(valueS.pop
) S.push( value
return value
:
, .
, .

" , .
, , ) . O(n

Queue
, ;
. ( FIFO: first in, first out ) .




) (MakeEmptyQueue
)(IsEmptyQueue
)enqueue(x
dequeue() value

:

:
N .
Q N
Size .
Head ) (.
Tail ) (.
)(isEmpty
)return (size==0

)(Object dequeue
)(if isEmpty
return ERROR
]data Q[head
head (head+1)%N
size size-1
return data

)createEmptyQueue(N
)Q new array[N
head 0
tail 0
size 0
)enqueue(data
)if (size==N
return ERROR
else
Q[tail] data
tail (tail+1)%N
size size + 1

:
)(isEmpty
)return (head == null
)enqueue(value
)L.addTail(value
)(dequeue
)(return L.removeHead
:
, . O(1) -
, , .
, .

Priority Queue
.Priority ;
. ) ( .





Maximum() value
Extract_Max() value
)enqueuePriorityQueue (x
)(MakeEmptyPriorityQueue
)(IsEmptyPriorityQueue

:

.
:
:1 , . , InsertionSort -
, .
, O(n ) : . O(1) :
:2 . . ,
) (.
, O(1) : . O(n ) :
:
{ class Item
;public int prior
;public Object data
;public Item next
{ )public Item(Object d, int pr, Item n
;data = d
;prior = pr
;next = n
}//constructor
}//class Item
{ class Priority_Queue
;Item head
{public Priority_Queue
;head = null
}//constructor

public void Enqueue(Object data, int priority) {


if ((head == null) || (head.prior < priority)) //new item is max
head = new Item( data, priority, head );
else {
Item current = head;
while ((current.next != null ) && ( priority < current.next.prior ) )
current = current.next;
current.next = new Item( data, priority, current.next );
}//else
}//Enqueue
public Object Extract_Max () {
if (head == null) {
System.out.println("Empty queue");
return NULL;
}
Object value = head.data;
head = head.next;
return value;
}// Extract_Max
public Object Maximum () {
if (head == null) {
System.out.println("Empty queue");
return NULL;
}
return head.data;
}// Maximum


Operation
Enqueue
extractMax
Maximum

O(1)

LinkedList
O(n)
O(1)
O(1)

O(n )
O(n)
O(1)

LinkedList
O(1)
O(n)
O(n)

O(n)
O(n)


Operation
Enqueue
extractMax
Maximum


.
( Node )
next
. prev
Class Item{
public Object data;
public Item next;
public Item prev;
}

. ,
addAfter( newNode, afterNode )
newNode.next = afterNode.next
newNode.prev = afterNode
if (afterNode.next != NULL)
afterNode.next.prev = newNode
else
Tail = newNode
afterNode.next = newNode

isEmpty()
return (head = =NULL)
( )
delete( node )
if node.next != NULL
node.next.prev = node.prev
else
tail = node.prev
if node.prev != NULL
node.prev.next = node.next
else
head = node.next

07 - Trees
:
, .
-.

:
: . A
: F B -
B. F -
: C , G C --
.G
: ) . .(... H , G , E
: . :
) . , F , B
.(... C
: G , A -
A -. G -
: B , J
- B -. J -
: , B - -
B B - ,
.

:
- v . v -
: .0
- v )( . v
: .0
- = .
:
, .0
, .
- .
: .0
: degree(v) - v

:
- ) (
:
) (

- , .
:

) ( Full Binary Tree


2.
:

) ( Complete Binary Tree


.
:


) (.
:

:
:

h 1
. h 1
h 1
. h 2
:
.

:
T n l , , h:

i ni = 2 i

l = nh = 2 h :

n = ni = 2 i = 2 h +1 1 :

h = log 2 (n + 1) 1 :

1) 2 = 2 1 = l 1 :

i =0

i =0

h +1

n l = (2

8 - Heap

: ) (
) (

)(Init
)Insert(value
)(Maximum
)(extractMaximum
)Build(A


)O(1
) O(log n
)O(1
) O(log n
) O(n


A .
A.length
heap_size
:
: 0
: 1
: 2
.
: .
:
0
16
2

1
11

12
5

6
10

4
9

7
9
1

10

12

11

:
parent (i ) = (i 1) / 2

8
8

left (i ) = 2i + 1
right (i ) = 2i + 2

0
16

:
:
, i > 0A[parent(i)] A[i] :
. A[0] -
)O(log n
: Heapify
,
.
:
v , .
v , .
v - , .
v - w . v
, w .

)Heapify(A, i
)L left(i
)R right(i
largest i
]if L < heap_size and A[L] > A[largest
largest L
]if R < heap_size and A[R] > A[largest
largest R
if largest i
]exchange a[i] A[largest
)Heapify(A, largest
:
O(h) = O(log n) :
:
.

: Build-Heap
, .
heapify , .
, n-1 . 0

heapify.
)BuildHeap(A
heap_size A.length
for i A.length / 2 -1 downto 0
)heapify(A, i
BuildHeap
: n , ) , O(log n ). O(n logn
:
BuildHeap . Heapify
:
h 1

h +1

, n = 2 . n -

:
i 2 . :
i

h 1

i =0

i =0

)S = 2i (h i ) = 2i (h i ) = h + 2(h 1) + 4(h 2) + ... + 2 h1 (1


:

)2S = 2h + 4(h 1) + 8(h 2) + ... + 2 (1


h

" :
h +1

1
= 2 h+1 2 h < n
2 1
:

S = 2S S = h + 2 + 4 + 8 + ... + 2 h = (h 1) +

Build-Heap )O(n

Priority Queue

:
)( Maximum
)( extractMaximum
) Insert(x x
)(: Maximum
]. A[0
O(1) :
)(: extractMaximum
] , A[0 .
]: A[0
] A[n-1A[0] -
.1-
, " heapify .

)(extractMaximum
if heap_size < 1
return ERROR
]max A[0
]A[0] A[heap_size -1
heap_size heap_size -1
)heapify(A, 0
return max

:
, ) . O(log n

: Insert()
.
.

:
16

16

/
14
/
8

\
10
\
7

/
9

/ \ / \
2 4 1 15

/
14
\
3

/
8

\
15
/ \ / \
2 4 1 7

16
\
10
/
9

/
15
\
3

/
8

\
14
/ \ / \
2 4 1 7

\
10
/
9

\
3

Insert(key)
heap_size heap_size +1
A[heap_size 1] key
i heap_size-1
while i > 0 && A[parent(i)] < A[i]
A[i] A[parent(i)]
i parent(i)

:
O(log n ) :
: , "
Array
O(n)
O(1)
O(1)

LinkedList
O(n)
O(1)
O(1)

Heap
O(log n)
O(log n)
O(1)

Operation
Insert
ExtractMax
Maximum

Heap-Sort -
.

, .
,1- .
.1
)HeapSort(A
)BuildHeap(A
for i A.length-1 downto 1
]exchange A[0] A[i
heap_size heap_size -1
)heapify(A, 0

:
) O(n , n heapify ) . O(log n
" ) . O(n log n
: in-place O(1) - .

09 Binary Trees

:( Java - )
class Node{
Object data;
Node left;
Node right;
Node(Object d, Node l, Node r ){
data = d;
left = l;
right= r;
}
public String toString(){
return "Node: " + data;
}
}
class BinaryTree{
Node root;
BinaryTree(){
root = null;
}
int getHeight(){
return getHeight(root);
}
int getHeight( Node node ){
if( node == null )
return -1;
int lheight = getHeight(node.left );
int rheight = getHeight(node.right );
return Math.max( lheight, rheight) + 1;
}

:
, .
:
: Pre-order
) ( pre


: Post-order


) ( post
) In-order (:

) ( inorder

, ) : :( BFS

, ) 1 (
, ) 2 (
...
: Java -
{class BinaryTree

{)(void printInOrder
) if( root == null
;) "System.out.println( "BinaryTree is empty
else
;)printInOrder(root
}
{)void printInOrder(Node node
)if (node == null
;return
;)printInOrder(node.left
;) )(System.out.println(node.toString
;)printInOrder(node.right
}

int getSize(){
return getSize(root);
}
int getSize(Node node){
// postorder
if (node == null)
return 0;
int lsize = getSize(node.left);
int rsize = getSize(node.right);
return lsize + rsize + 1;
}
void getPreOrder(){
getPreOrder(root);
}
void getPreOrder(Node node){
if (node == null)
return;
System.out.println(node.toString());
getPreOrder(node.left);
getPreOrder(node.right);
}
void printBFS(){
if( root == null )
return;
Queue q = new Queue();
q.enqueue( root );
while( !q.isEmpty() ){
Node curr = (Node)q.dequeue();
if( curr.left != null )
q.enqueue( curr.left );
if( curr.right != null )
q.enqueue( curr.right );
System.out.print( curr.data + "," );
}
}
}

. O(n ) , n T

10 Binary Search Tree


1, 3, 7, 8, 11, 20 : in-order

:
Search
Insert
Delete
Maximum
Minimum
Predecessor
Successor
isEmpty

:
class Node{
int key;
Object data;
Node left;
Node right;
Node parent;
Node( int newKey, Object newData ){
key = newKey;
data = newData;
left = null;
right= null;
parent = null;
}
Node( int newKey, Object newData, Node par ){
key = newKey;
data = newData;
left = null;
right = null;
parent = par;
}
}
class BinarySearchTree{
Node root;
BinarySearchTree(){
root = null;
}
.
}

: search(x)

Object search( int searchKey){


return search( searchKey, root );
}
Object search(int searchKey, Node node){
if (node == null)
return null;
if(searchKey == node.key)
return node.data;
if (searchKey < node.key)
return search( searchKey, node.left);
else
return search( searchKey, node.right);
}
O(h)
:
.
. left
Object minimum(Node node){
if( node==null )
return null;
while (node.left != null)
node = node.left;
return node.data;
}
O(h)

void insert( int key, Object data ){


if( root == null )
root = new Node( key, data );
else
insert( key, data, root );
}
void insert( int newKey, Object newData , Node node){
if(newKey < node.key ) {
if( node.left == null )
node.left = new Node( newKey, newData, node);
else
insert( newKey, newData, node.left);
}
else {
if( node.right = = null )
node.right = new Node( newKey, newData, node);
else
insert( newKey, newData, node.right );
}
}
O(h)
successor and predecessor \
. x - x
. x - x
. x - \

Node successor( Node node ) {


if (node.right != null)
return minimum(node.right);
Node tmp = node.parent;
while ((tmp != null) && (node == tmp.right)) {
node = tmp;
tmp = tmp.parent;
}
return tmp;
}
O(h)

:
w , v - w . w

void replaceChild(Node node, Node curChild, Node newChild ){


if( node.left == curChild )
node.left = newChild;
if( node.right == curChild )
node.right = newChild;
if( newChild != null )
newChild.parent = node;
}
Object delete( int key ){
Node toDelete = find( key, root);
if( toDelete == null )
return null;
Object data = toDelete.data;
delete( toDelete );
return data;
}
void delete( Node toDelete ){
if( toDelete.right == null ){
if( toDelete != root )
replaceChild(toDelete.parent, toDelete, toDelete.left);
else
root = toDelete.left;
}
else if( toDelete.left == null ){
if( toDelete != root )
replaceChild(toDelete.parent, toDelete, toDelete.right);
else
root = toDelete.right;
}
else{ Node successor = toDelete.right;
while( successor.left != null )
successor = successor.left;
toDelete.copy( successor );
delete( successor );
}
}
O(h)

11 -
Huffman Code
: ?
:
a, b, c, d, e .0.12, 0.4, 0.15, 0.08, 0.25 100,000 .

8 :Ascii 800,000

' ' '' 0?1-


) ( , 3 :
000
a
001
b
010
c
011
d
100
e

- '' ,'' )
(.

000

0000

000

001
010

0.12

0.4

11

0.15

01

001

0.08

001

0001

011

0.25

10

01

100

?
: 1
3*12,000 + 2*40,000 + 2*15,000 + 3*8,000 + 2*25,000 = 220,000
:2
4*12,000 + 1*40,000 + 3*15,000 + 4*8,000 + 2*25,000 = 215,000
:3
3 * 100,000 = 300,000
Prefix Code -
: .
: , .
: abc 00001001 :2
: bce 100101 :2





:
T

:
T
decoder

cC c
Q )f(c

) - f(c
)x Extract(Q
)y Extract(Q
z
left(z) x
right(z) y
)f(z) f(x) + f(y
z
) - return Extract(Q T-
)Huffman (C
|n |C
QC
for i 1 to n-1
z new Node
)(x Q.ExtractMin
)(y Q.ExtractMin
)f(z) f(x) + f(y
z.left x
z.right y
)Q.Insert(z
)(return Q.ExtractMin

)O(n)*O(log n) = O(n logn

f: 1100

e: 1101

d: 111

c: 100

b: 101

a: 0

0 , -1 ,.

T prefix code c- C ) - f(c , c
) d(c - B(T) , c
)B (T ) = f (c)dT (c
cC

Decoding

12 - AVL Tree
AVL , , Adelson-Velskii and Landis ,
. ,
- ,1 - ) . O(logn
). O(logn
: AVL
AVL , ) . O(log n
AVL :
, 1- .
p T : height( p.left ) height ( p.right ) 1 :

:
3
2
0

44
1
17

78
1

88

50
0

32
0

62

48

AVL
:
O(log n ) AVL
:
Let us bound n(h): the minimum number of internal nodes of an AVL tree of
height h.
We easily see that n(0) = 1 and n(1) = 2
For h 2, an AVL tree of height h contains the root node, one AVL subtree of
height h-1 and another of height h-2.
That is, n(h) = 1 + n(h-1) + n(h-2)
Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2).
So n(h) > 2n(h-2) > 4n(h-4) > 8n(n-6) (by induction),
n(h) > 2kn(h-2k)
after k = h/2 steps:
n(h) > 2 h/2
Taking logarithms: h/2 < log n(h) h < 2 log n(h)
Thus the height of an AVL tree is O(log n).

:AVL
.
,
. z x
: ( x ) y - " z -
height ( y ) = height ( sibling ( y )) + 2
:
z o
z y o
y x o
44
1

17
32

50
0

48

3
78

88

x
62

T3

54

T2

T0
T1

Single rotations:
right-rotation(z)

c=z

b=y

single rotation

b=y

a=x

c=z

a=x
T3
T0

T3

T2

T2

T1

T0

T1

left-rotation(z)

a=z

b=y

single rotation
b=y

a=z

c=x

c=x
T0

T3

T1

T3

T0

T1

T2

T2

Double rotations:
right-rotation(y)
left-rotation(z)

double rotation

a=z
c=y

b=x
a=z

c=y

b=x
T0

T3

T2

T0

T2

T1

T3

T1

left-rotation(y)
right-rotation(z)

double rotation

c=z

b=x
a=y

a=y

c=z

b=x
T3

T0
T2
T1

T3

T2

T1

T0

44
1

17
32

50
0

48

3
78

88

x
62

T3

54

T2

T0
T1

44

1
17
32

62

78

50

z
1

0
48

54

88

T2
T0

T1

T3

: Remove

BST
.
!Oh no, unbalanced
y

62

78

17
50

0
88

54

1
0

T1

y
z

44

78
1

50

88

T2

54

17

48

T3

: )O(log n

T0
32

T3

48

T2

62

44

T0

class AVLNode{
AVLNode lChild, rChild, parent;
int key;
Object data;
int height;
AVLNode( int newKey, Object newData){
key = newKey;
data = newData;
lChild = null;
rChild = null;
parent = null;
height = 0;
}
AVLNode( int newKey, Object newData, AVLNode p ){
key = newKey;
data = newData;
lChild = null;
rChild = null;
parent = p;
height = 0;
}
void setHeight(){
int lHeight = -1, rHeight = -1;
if( lChild != null )
lHeight = lChild.height;
if( rChild != null )
rHeight = rChild.height;
height = Math.max( lHeight, rHeight ) + 1;
}
void copy( AVLNode n ){
key = n.key;
data = n.data;
}

class AVLTree{
AVLNode root;
AVLTree(){
root = null;
}
void insert( int key, Object data ){
if( root = = null )
root = new AVLNode( key, data);
else
insert( key, data, root);
}
void insert(int newKey, Object newData, AVLNode node ){
if( node.key > newKey ){
if( node.lChild = = null )
node.lChild = new AVLNode( newKey, newData, node);
else
insert( newKey, newData, node.lChild );
}
else{
if( node.rChild = = null )
node.rChild = new AVLNode( newKey, newData, node);
else
insert( newKey, newData, node.rChild);
}
checkAVL(node);
}
void checkAVL(AVLNode node){
int lHeight , rHeight;
lHeight = getHeight(node.lChild);
rHeight = getHeight(node.rChild);
if( Math.abs( lHeight - rHeight ) <= 1 ) //The AVL property holds
node.height = Math.max( lHeight, rHeight ) + 1;
else //The AVL property has been violated - a fix is needed
fixAVL( node, lHeight, rHeight );
}
int getHeight( AVLNode n){
if( n = = null )
return -1;
return n.height;
}

void replaceChild(AVLNode node, AVLNode curChild, AVLNode newChild ){


if( node.lChild = = curChild )
node.lChild = newChild;
else if( node.rChild = = curChild )
node.rChild = newChild;
if( newChild != null )
newChild.parent = node;
}
Void leftRotation(AVLNode a){
AVLNode b = a.rChild;
if ( a.parent != null )
replaceChild(a.parent, a, b);
else{
root = b;
b.parent = null;
}

void rightRotation(AVLNode a){


AVLNode b = a.lChild;
if ( b.parent != null )
replaceChild(a.parent, a, b);
else{
root = b;
b.parent = null;
}

replaceChild(a, a.rChild, b.lChild);


b.lChild = a;
a.parent = b;

replaceChild(a, a.lChild, a.rChild);


b.rChild = a;
a.parent = b;

a.setHeight();
b.setHeight();

a.setHeight();
b.setHeight();
}

void fixAVL(AVLNode node, int lHeight, int rHeight ){


if( lHeight > rHeight ){ //The balance tips to the left
if( getHeight( node.lChild.lChild ) > getHeight( node.lChild.rChild ) ){
//case 1
rightRotation(node);
}
else if( getHeight( node.lChild.lChild ) < getHeight( node.lChild.rChild ) ){
//case 2
leftRotation(node.lChild);
rightRotation(node);
}
}
if( rHeight > lHeight ){
if( getHeight( node.rChild.rChild ) > getHeight( node.rChild.lChild ) ){
//case 3
leftRotation(node);
}
else if( getHeight( node.rChild.rChild ) < getHeight( node.rChild.lChild ) ){
//case 4
rightRotation(node.rChild);
leftRotation(node);
}
}
}

13-Disjoint Sets

. "
. :
) - MakeSet( x )( x
) - FindSet( x x
) - Union( x, y xy-
.
. FindSet , Union
.


, .
.
.
c

) (union .
h

MakeSet FindSet - ) O(1 Union


.
n ) O(n2 .


.
.
Union .
n : Union
n . x
x - . x . Union
. x
. x
. k 2k
. n x logn
. n ). O(nlogn

.
.
" . ,
.
a

h
a

h
MakeSet Union - ) O(1 FindSet
. ). O(n


- .
Union .
) MakeSet( x
p( x )x
height( x )0
) Union( x, y
) ) if( height( x ) > height( y
p( y )x
else
p( x )y
) ) if(height ( x ) = height ( y
height ( y ) height(y) + 1
) FindSet( x
) if( p( x ) x
) ) FindSet( p( x
) return p( x
) O(log n FindSet ). O(log n

Path compression -
FindSet .
:
a

) : FindSet( f
a

: FindSet
) FindSet( x
) if( p( x ) x
) ) p( x )FindSet( p( x
) return p( x
FindSet : ,
.
- m Union by height Path compression -
)) , O(m(m,n
. (m,n)<=4 ,m
FindSet ).O(1

14- Hashing and Direct Address Tables


\
)( ,
Search Insert Delete :
.
:

) O(n
) O(n

)O(1
)O(1

) O(n
) O(n
)AC = (1+2+3++n)/n = n(n+1)/2n = O(n
)Direct Address Tables (DA
:
key - . .
, , ..
U U={0,1,... ,m-1}. |U |=m
) (DA table .
]DA table: T[0m-1
:
)DA_Delete(T, key
T[key] = null
:

)DA_Insert(T, x
T[x.key] = x

)O(1
)O(1
)O(1

:
.
.
.


)O(1
)O(1
)O(1

)DA_Search(T, key
]return T[key

:1

,
:1
. . 9- , 10 ,
10 7 - , 100 .
9

:2
)( 24 ,30
23 30 - .
.

\ Hash Table
" ) .( Direct Addressing .
, " ) ( Hash function
.
: , O(1) -.
: HASH
.:
}h : KEYS {0...m 1
:

h(k ) = k mod10

. m = 10

:
)Delete(T, key
T[h(key)] = null

)Insert(T, x
T[h(x.key)] = x

)Search(T, key
])return T[h(key

Hashing , ,:


Search
)(1
) (n
Insert
)(1
)(1
Delete
)(1
) (n
:
Chaining
Open Addressing
:
.
.

:
) h( x.key
(1) :
x.key ) h( x.key
)O(list size
list size
.
:
+ 1 :
2

- (list size + 1) : .
) , h( x.key .
: + .

Load Factor :
.
= n/m
, Chaining - .1-
: Chaining

) . (n
.
.
, h .
, ) .( Simple uniform hash
.
,
.
:
O(1) :
O(1+) :
O(1+) :
n , m
) n = O(m , ) = O(1 ) O(1.
:
n = 2100 U , 700
,3 .

:Open Addressing
:



.
o ,
o , .
, ) , (

: HASH

}h : KEYS {0...m 1} {0...m 1

, .
)Hash_search(T, k
i= 0
repeat
)j = h(k, i
if T[j].key = k
return x
else
i = i+1
until T[j] = null or i = m
return null

)Hash_insert(T, x
i0
repeat
)j h(x.key, i
if T[j] = null
T[j] x
return
else
i i+1
until i = m

: open-addressing -
.
" .deleted
, , deleted
.
, deleted ,
.

open addressing:
2 :
linear probing
- double probing " HASH.
Linear probing
HASH :
h(key, i ) = (h(key ) + i ) mod m
:
, , : m

:
" 12.62-
)12 (2, 3, 4, 5, 6, 7, 8, 9, 0,1
)62 (2, 3, 4, 5, 6, 7, 8, 9, 0,1
, m .
: Linear Probing
LP .
LP ,
. LP
.
,
.

:
, m = 2n = 1/2,
Tsearch = 1 1 + 1 2 = 3
2
2
2

,
m m
m
m

m
n
= * 1 + + ( 1) + ( 2) + ... + 2 / m = O = O
2
2
2
2

8
4

Tsearch

: Double probing
HASH HASH:
h(key, i ) = (h1 (key ) + i h2 (key )) mod m
:
, h2 (key ) - , : m
:
h1(key) = key mod 13
h2(key) = key mod 11 + 1
m = 13
26, 39, 20 :

26
39
20

h1
h1(26) = 0
h1(39) = 0
h1(20) = 7

h2
h2(26) = 5
h2(39) = 7
h2(20) = 10


0, 5, 10, 2, 7,
0, 7, 1, 8, 2,
7, 4, 1, 11,

:
12

11

10

7
39

4
20

0
26

:
- ) ( h1 ) ( h2 m
".
2

:
m h2 (key ) - ,
.
:
m
m = 2 k h2 - - .
...

: )(uniform hashing
)(

!m

.1

:
, m=3 , m!=6
(0, 1, 2) (0, 2, 1) (1, 2, 0) (1, 0, 2) (2, 1, 0) (2, 0, 1) :
h k . 1/6
:
)a12 = (2, 3, 4, 5, 6, 7, 8, 9, 0,1
)a62 = (2, 3, 4, 5, 6, 7, 8, 9, 0,1
, m
: Double Hashing
- m2.
: Open Addressing
:
, .
.
i - . i
i ) . i 1 (1
1 .
m

- ) . U (n ) = i i 1 (1
i =1

i =1

i =1

i =1

U (n ) = i i 1 (1 ) i i 1 (1 ) = (1 ) i i 1 = (1 )S

S = i i 1 :
i =0

S = 1 + 2 + 3 + ...
1 1 + 2 2 + 3 3 + ...
= S
2

:
1
1

= (1 )S = 1 + + 2 + 3 + ...

1
O
:
1
) O(1.

: O(1) + .

HASH
: HASH


: m
: m -
2 .10 2 log 2 m
) .( LSB ,10 log 10 m .

.
.2 2
,2 . 2 8 = 256
:
- ""
.

0<a<1
k .a

m - .
h(k ) = m (ak mod 1) :
m
a 5 1 / 2 0.61803 ...

:1
m = 10000 k = 123456
:
= )h(k ) = 10000 (123456 0.61803... mod 1
)= 10000 (76300.0041151... mod 1
= 10000 0041151...
= 41.151...
= 41
:2
a = 0.81, m = 10, key = 7
a key = 5.67
a key mod 1 = 0.67
h(7) = |_ 10 * 0.67_| = 6

=a

15 - Graphs

: ,0 )n*n = O(n2

}V = {1, 2, 3, 4, 5, 6
})E = {(1, 2), (1, 5), (2, 5), (3, 6
n = 6, m = 4
: ,0 )n*(n-1)/2 = O(n2


''
-

n


:
:

''
-

n x n
) (i, j A[i, j] = 1

:
:

16 - Topological Sort

Directed Acyclic Graph DAG


.
G = (V , E ) DAG
V
(u , v) E u -
v.
G DAG
.


.

DAG - .
.
) , ( ) ,
( . .

'

)(1 5 3 4 2 6), (1 3 4 5 2 6), (3 1 4 5 2 6), (3 4 1 5 2 6

O(n+m)
O(n(n+m))

O(n2(n+m))

O(n+m) +

O(n+m) +

17 -
BFS Breadth First Search
(E,V)=G ) s V (
vV s -
s - v - s -. v -
: s - k
k+1
:








SHALOMRAVSHUVECH
SLAHUHSVARMOHCEV

s, uV
visited :
u.p :
u -
null u=s u
: u.d s -u -

:
s
:
-

BFS(G, s)
Q makeEmptyQueue()
for each vertex u from V - {s}
u.visited false
u.d
u.p null
s.visited true
s.d 0
s.p null
Q.enqueue(s)
while Q.IsEmpty() = = false
u Q.Dequeue()
for each v from Adj(u)
if v.visited = = false
v.visited true
v.d u.d +1
v.p u
Q.Enqueue ( v )

O(|V|) :
: 2
)( . ) |O(|V
. )|O(|E
)|O(|V|+|E
Breadth First Tree

) G=(V, E
V={vV : p(v) null} s
}) E={(p(v), v)E : v (V / s
s

Path - Breadth First Tree
) Print-Path ( G, s, v
if v = = s
print s
else if v.p = = null
"print "no path from s to v exists
else
)Print-Path ( G, s, v.p
print v
:
v.d s - . v
:
v.p s - . v

s - .
:

) {(v. p v ) | v. p null }vV (G

18 -
MST- Minimal Spanning Tree

'' .
> p=<v0, v1, ... vk

) w( p ) = w(vi 1 , vi
i =1

u - v -

min{w( p) : u
} v
p

otherwise

MST- Minimal Spanning Tree

= ) (u , v

:
. key
. key
. v p(v)
Prim-MST(G, r)
PQ makePriorityQueue()
for each v V-r
v.key
r.key 0
r.p null
PQ.enqueue(V)
while !PQ.isEmpty()
u PQ.extractMin()
for each v adjacent(u)
if (v PQ and w(u, v) < v.key)
v.p u
v.key w(u, v)
Q.decreaseKey(v, v.key)
return T = { (v, v.p) : v V-r }

Start at a:
Q
p
key
pres

a b c d e f g h
- - - - - - - 0
x

Q = Q {a}

Q = Q {b}

Q
a b c d e f g h
p
- a - a a - - key 0 1
6 3
pres x
Q = Q {e}
Q
p
key
pres

a
0
x

Q
p
key
pres

a
0
x

b c d e f g h
a b a a - - 1 6 6 3
x

b c d e f g h
a e e a - e e
1 3 2 3
2 8
x
x

Q = Q {d}

Q = Q {g}

Q
a b c d e f g h
p
- a e e a - e e
key 0 1 3 2 3
2 8
pres x x
x x
Q = Q {c}

Q
a b c d e f
p
- a e e a g
key 0 1 3 2 3 4
pres x x
x x
Q = Q {f}

Q
a b
p
- a
key 0 1
pres x x
Q = Q {h}
Q
p
key
pres

a
0
x

b
a
1
x

c
e
3
x

d
e
2
x

e f
a g
3 4
x

g h
e g
2 7
x

c
e
3
x

d
e
2
x

e
a
3
x

g
e
2
x

f
g
4
x

Q
p
key
pres

a
0
x

b
a
1
x

c
e
3
x

d
e
2
x

e
a
3
x

f
g
4
x

g h
e g
2 7
x
g h
e g
2 7
x

h
g
7
x

:
O(n) : ''
O(n logn) : :
O(m logn) : ,
O(n) + O(n logn) + O(m logn) = O((n + m) logn) = O(m logn) :''

:
-
.
.
MST - .
V 1 .
:

, .
, .
.
.
) . O(V

Finding minimum spanning tree


The algorithm starts with V different trees (the vertices in V) and each time unions the trees by adding
the next minimal weight edge that doesnt creates a cycle.

)MST-Kruskal(G, w
T
for each vertex v V
)Make-Set(v
sort the edges of E by non-decreasing weight w
for each edge (u,v) E in order by non-decreasing weight
)if Find-Set(u) Find-Set(v
})T T {(u,v
)Union(u, v
return T

Complexity:
O( E log E + V ) = O( E log E ) , Assuming that the optimized union-find was used.

A
7

18

10
9

13

14

10
9

12

12

11

11
D

23

23
A

A
7

18

10
9

13

14

10
9

12

12

11

11
D

23

23
A

A
7

18

10
9

3
G

12

10
E
11
D

23

23
A
7
6

3
G

B
4

13

14

10
9

12
11
D
23

12

13

14

11

18

B
4

13

14

18

B
4

13

14

18

B
4

13

14

18

19 -
Dijkstra Algorithm
) , G = (V , E ,W .
: . s
v :
d s -v -
p v s-

:
)Initialize-Single-Source (G, s
for each vertex v in V
= v.d
v.p = null
s.d = 0
:
v " . u
)Relax(u, v
)if v.d > u.d + w(u,v
)v.d u.d + w(u,v
v.p u
2
5
6

2
5
9

2
5

5
7
2

:
, S s -
} {V - S Q
.
S = s .0
Q S -
o , Q - .S -
o Q - s -
.

:S

Dijkstra (G, s)
Initialize_Single_Source( G, s )
S
PQ V
while !PQ.IsEmpty
u PQ.ExtractMin()
SSu
for each vertex v Adj( u )
Relax(u, v)

:Q


.1 "
)|O(|V
" )O(|V|2+|E|)=O(|V|2
.2 "
)|O(log|V
" )|O((|V|+|E|) log(|V
" .

.
:
) (s, u s -. u -
uV u S - u.d
s - , u - ). u.d= (s, u
u - S - . u.d (s, u) - us S
.
s -. u -
S
u

xy

p=s

x : xS
y yS
) x.d = (s, x u - .
x S ) (x, y y
) .y.d = (s, y y - u s - ). (s, y) (s, u
y.d = (s, y) (s, u) u.d
u S . u.d y.d
- ) u.d= (s, u .


Shortest Paths on a DAG

DAG-Shortest-Paths(G, s)
Topological-Sort(G)
Initialize-Single-Source(G, s)
for each vertex u in topological order
for each v Adj(u)
Relax(u, v)
s, r, t, w, u, v :
s

s
0

2
w

7
5

5
v

w
5

9
11

7
5

s
0

8
11

Dijkstras properties
Triangle inequality: For any edge (u,v) in E, (s,v) (s,u) + w(u,v)
Upper-bound property: We always have v.d (s,v) for all vertices v in V, and once
v.d achieves the value (s,v) it never changes.
No-path property: If there is no path from s to v, than we always have
v.d = (s,v) =
Convergence property: If s to u followed by edge (u,v) is a shortest path from s to u
and v, and if u.d = (s,u) at any time prior to relaxing edge (u,v), then v.d = (s,v)
after relaxation.
Path-relaxation property: If p = <v0, v1, , vk> is a shortest path from s = v0 to vk,
and the edges of p are relaxed in order (v0,v1), (v1,v2), (vk-1, vk), then
vk.d = (s, vk), regardless of any other relaxation steps, even if they are intermixed
with relaxations of edges in p.
Predecessor-subgraph property: Once v.d = (s,v) for all v in V, the predecessor
subraph is a shortest-paths tree rooted at s.


.1 A n . A -
, log n A ) . O ( n log log n
logn
.2 n :
) ( .
.inside
.a . O(n) -
) (Item .
.b .O(n) -
.c . O(n2) -
.
.d . O(n logn) -
. .

head

inside

.3 ,
.
.4
)Sort (A, start, end
)mid median(A
s start
e end
while s < e
while A[s] < mid
s++
while A[e] > mid
e -if s < e
)]swap (A[s], A[e
)Sort (A, start, s
)Sort (A, s+1, end
)Compute run time if function median is executed in O(1
)Compute run time if function median is executed in O(logn), O(n
)Compute run time if function median is executed in O(nlogn
?Is this the inplace sort

a.
b.
c.
d.

e. Is this the stable sort?


. . " n .5
. .
. - k - .
O(n + klogn) :
6. A graph G = (V, E) is called a bipartite graph if V = V1 U V2 and V1 V2 = and
all edges in E connect between a node in V1 and a node in V2 . Given a connected
graph G = (V, E) describe an algorithm that decides if G is bipartite and runs in time
O(|V|+|E|)
7. How can you recognize a cycle in an undirected graph in O(|V|+|E|) ?