Introduction to Data Structures
and Algorithms
Pham Quang Dung and Do Phan Thuan
Computer Science Department, SoICT,
Hanoi University of Science and Technology.
July 8, 2016
1 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
First example
Find the longest subsequence of a given sequence of numbers
Given a sequence s = ha1, . . . , ani
a subsequence is s(i, j) = hai , . . . , aj i, 1 ≤ i ≤ j ≤ n
weight w(s(i, j)) =
j
X
k=i
ak
Problem: find the subsequence having largest weight
Example
sequence: -2, 11, -4, 13, -5, 2
The largest weight subsequence is 11, -4, 13 having weight 20
2 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Direct algorithm
Scan all possible subsequences n
2

= n2+n
2
Compute and keep the largest weight subsequence
1 public long algo1(int [] a){
2 int n = a.length;
3 long max = a[0];
4 for(int i = 0; i  n; i++){
5 for(int j = i; j  n; j++){
6 int s = 0;
7 for(int k = i; k = j; k++)
8 s = s + a[k];
9 max = max  s ? s : max;
0 }
1 }
2 return max;
3 }
3 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Direct algorithm
Faster algorithm
Observation:
Pj
k=i a[k] = a[j] +
Pj−1
k=i a[k]
1 public long algo2(int [] a){
2 int n = a.length;
3 long max = a[0];
4 for(int i = 0; i  n; i++){
5 int s = 0;
6 for(int j = i; j  n; j++){
7 s = s + a[j];
8 max = max  s ? s : max;
9 }
0 }
1 return max;
2 }
4 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Recursive algorithm
Divide the sequence into 2 subsequences at the middle s = s1 :: s2
The largest subsequence might
I be in s1 or
I be in s2 or
I start at some position of s1 and end at some position of s2
Java code:
1 private long maxSeq(int i, int j){
2 if(i == j) return a[i];
3 int m = (i+j)/2;
4 long ml = maxSeq(i,m);
5 long mr = maxSeq(m+1,j);
6 long maxL = maxLeft(i,m);
7 long maxR = maxRight(m+1,j);
8 long maxLR = maxL + maxR;
9 long max = ml  mr ? ml : mr;
0 max = max  maxLR ? max : maxLR;
1 return max;
2 }
3 public long algo3(int [] a){
4 int n = a.length;
5 return maxSeq (0,n -1);
6 }
5 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Recursive algorithm
1 private long maxLeft(int i, int j){
2 long maxL = a[j];
3 int s = 0;
4 for(int k = j; k = i; k--){
5 s += a[k];
6 maxL = maxL  s ? maxL : s;
7 }
8 return maxL;
9 }
0 private long maxRight(int i, int j){
1 long maxR = a[i];
2 int s = 0;
3 for(int k = i; k = j; k++){
4 s += a[k];
5 maxR = maxR  s ? maxR : s;
6 }
7 return maxR;
8 }
6 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Dynamic programming
General principle
Division: divide the initial problem into smaller similar problems
(subproblems)
Storing solutions to subproblems: store the solution to subproblems
into memory
Aggregation: establish the solution to the initial problem by
aggregating solutions to subproblems stored in the memory
7 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Dynamic programming
Largest subsequence
Division:
I Let si be the weight of the largest subsequence of a1, . . . , ai ending at
ai
Aggregation:
I s1 = a1
I si = max{si−1 + ai , ai }, ∀i = 2, . . . , n
I Solution to the original problem is max{s1, . . . , sn}
Number of basic operations is n (best algorithm)
8 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Dynamic programming
1 public long algo4(int[] a){
2 int n = a.length;
3 long max = a[0];
4 int[] s = new int[n];
5 s[0] = a[0];
6 max = s[0];
7 for(int i = 1; i  n; i++){
8 if(s[i-1]  0) s[i] = s[i-1] + a[i];
9 else s[i] = a[i];
0 max = max  s[i] ? max : s[i];
1 }
2 return max;
3 }
9 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analyzing algorithms
Resources (memory, bandwithd, CPU, etc.) required by the algorithms
Most concern is the computational time
Input size: number of items in the input
Running time: measured in term of the number of primitive
operations performed
10 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analyzing algorithms
algo1: T(n) = n3
6 + n2
2 + n
3
algo2: T(n) = n2
2 + n
2
algo3:
I Count the number of addition (”+”) operation T(n)
T(n) =
(
0 if n = 1
T(n
2 ) + T(n
2 ) + n if n  1
I By induction: T(n) = n log2n
algo4: T(n) = n
11 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analyzing algorithms
Worst-case running time: the longest running time for any input of
size n
Best-case running time: the shortest running time for any input of
size n
Average-case running time: probabilistic analysis (make assumption of
a distribution of the input) yields expected running time
12 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Order of growth
Consider only leading term of the function
Ignore constant coefficient
Example
I an3
+ bn2
+ cn + d = Θ(n3
)
13 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Asymptotic notations
Given a fucntion g(n), we denote:
I Θ(g(n)) = {f (n) : ∃c1, c2, n0 s.t. 0 ≤ c1g(n) ≤ f (n) ≤ c2g(n), ∀n ≥
n0}
I O(g(n)) = {f (n) : ∃c, n0  0 s.t. f (n) ≤ cg(n), ∀n ≥ n0}
I Ω(g(n)) = {f (n) : ∃c, n0  0 s.t. cg(n) ≤ f (n), ∀n ≥ n0}
Examples
I 10n2
− 3n = Θ(n2
)
I 10n2
− 3n = O(n3
)
I 10n2
− 3n = Ω(n)
14 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analysis of algorithms
Experiments studies
Write a program implementing the algorithm
Execute the program on a machine with different input sizes
Measure the actual execution times
Plot the results
15 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analysis of algorithms
Shortcomings of experiments studies
Need to implement the algorithm, sometime difficult
Results may not indicate the running time of other input not
experimented
To compare two algorithms, it is required to use the same hardware
and software environments.
16 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analysis of algorithms
Asymptotic algorithm analysis
Use high-level description of the algorithm (pseudo code)
Determine the running time of an algorithm as a function of the input
size
Express this function with asymptotic notations
17 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analysis of algorithms
Sequential structure: P and Q are two segments of the algorithm
(the sequence P; Q)
I Time(P; Q) = Time(P) + Time(Q) or
I Time(P; Q) = Θ(max(Time(P), Time(Q)))
for loop: for i = 1 to m do P(i)
I t(i) is the time complexity of P(i)
I time complexity of the for loop is
Pm
i=1 t(i)
18 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analysis of algorithms
while (repeat) loop
Specify a function of variables of the loop such that this function
reduces during the loop
To evaluate the running time, we analyze how the function reduces
during the loop
19 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analysis of algorithms
Example: binary search
Function BinarySearch(T[1..n], x)
begin
i ← 1; j ← n;
while i  j do
k ← (i + j)/2;
case
x  T[k]: j ← k − 1;
x = T[k]: i ← k; j ← k; exit;
x  T[k]: i ← k + 1;
endcase
endwhile
end
20 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Analysis of algorithms
Example: binary search
Denote
d = j − i + 1 (number of elements of the array to be investigated)
i∗, j∗, d∗ respectively the values of i, j, d after a loop
We have
If x  T[k] then i∗ = i, j∗ = (i + j)/2 − 1, d∗ = j∗ − i∗ + 1 ≤ d/2
If x  T[k] then j∗ = j, i∗ = (i + j)/2 + 1, d∗ = j∗ − i∗ + 1 ≤ d/2
Ifx = T[k] then d∗ = 1
Hence, the number of iterations of the loop is dlogne
21 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Master theorem
T(n) = aT(n/b) + cnk with a ≥ 1, b  1, c  0 are constant
If a  bk, then T(n) = Θ(nlogba)
If a = bk, then T(n) = Θ(nklogn) with logn = log2n
If a  bk, then T(n) = Θ(nk)
Example
T(n) = 3T(n/4) + cn2 ⇒ T(n) = Θ(n2)
T(n) = 2T(n/2) + n0.5 ⇒ T(n) = Θ(n)
T(n) = 16T(n/4) + n ⇒ T(n) = Θ(n2)
T(n) = T(3n/7) + 1 ⇒ T(n) = Θ(logn)
22 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Sorting
Put elements of a list in a certain order
Designing efficient sorting algorithms is very important for other
algorithms (search, merge, etc.)
Each object is associated with a key and sorting algorithms work on
these keys.
Two basic operations that used mostly by sorting algorithms
I Swap(a, b): swap the values of variables a and b
I Compare(a, b): return
F true if a is before b in the considered order
F false, otherwise.
Without loss of generality, suppose we need to sort a list of numbers
in nondecreasing order
23 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
A sorting algorithm is called in-place if the size of additional memory
required by the algorithm is O(1) (which does not depend on the size
of the input array)
A sorting algorithm is called stable if it maintains the relative order
of elements with equal keys
A sorting algorithm uses only comparison for deciding the order
between two elements is called Comparison-based sorting
algorithm
24 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Insertion Sort
At iteration k, put the kth element of the original list in the right
order of the sorted list of the first k elements (∀k = 1, . . . , n)
Result: after kth iteration, we have a sorted list of the first kth
elements of the original list
1 void insertion_sort (int a[], int n){
2 int k;
3 for(k = 2; k = n; k++){
4 int last = a[k];
5 int j = k;
6 while(j  1  a[j-1]  last ){
7 a[j] = a[j -1];
8 j--;
9 }
0 a[j] = last;
1 }
2 }
25 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Selection Sort
Put the smallest element of the original list in the first position
Put the second smallest element of the original list in the second
position
Put the third smallest element of the original list in the third position
...
1 void selection_sort (int a[], int n){
2 for(int k = 1; k = n; k++){
3 int min = k;
4 for(int i = k+1; i = n; i++)
5 if(a[min]  a[i])
6 min = i;
7 swap(a[k],a[min ]);
8 }
9 }
26 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Bubble sort
Pass from the beginning of the list: compare and swap two adjacent
elements if they are not in the right order
Repeat the pass until no swaps are needed
1 void bubble_sort(int a[], int n){
2 int swapped;
3 do{
4 swapped = 0;
5 for(int i = 1; i  n; i++)
6 if(a[i]  a[i+1]){
7 swap(a[i],a[i+1]);
8 swapped = 1;
9 }
0 }while(swapped == 1);
1 }
27 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Merge sort
Divide-and-conquer
Divide the original list of n/2 into two lists of n/2 elements
Recursively merge sort these two lists
Merge the two sorted lists
28 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Merge sort
1 void merge(int a[], int L, int M, int R){
2 // merge two sorted list a[L..M] and a[M+1..R]
3 int i = L;// first position of the first list a[L.
4 int j = M+1;// first position of the second list a
5 for(int k = L; k = R;k++){
6 if(i  M){// the first list is all scanned
7 TA[k] = a[j]; j++;
8 }else if(j  R){// the second list is all scanne
9 TA[k] = a[i]; i++;
0 }else{
1 if(a[i]  a[j]){
2 TA[k] = a[i]; i++;
3 }else{
4 TA[k] = a[j]; j++;
5 }
6 }
7 }
8 for(int k = L; k = R; k++) 29 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Merge sort
1 void merge_sort(int a[], int L, int R){
2 if(L  R){
3 int M = (L+R)/2;
4 merge_sort(a,L,M);
5 merge_sort(a,M+1,R);
6 merge(a,L,M,R);
7 }
8 }
30 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Quick sort
Pick an element, called a pivot, from the original list
Rearrange the list so that:
I All elements less than pivot come before the pivot
I All elements greater or equal to to pivot come after pivot
Here, pivot is in the right position in the final sorted list (it is fixed)
Recursively sort the sub-list before pivot and the sub-list after pivot
31 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Quick sort
1 void quick_sort(int a[], int L, int R){
2 if(L  R){
3 int index = (L+R)/2;
4 index = partition(a,L,R,index );
5 if(L  index)
6 quick_sort(a,L,index -1);
7 if(index  R)
8 quick_sort(a,index +1,R);
9 }
0 }
32 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Quick sort
1 int partition(int a[], int L, int R, int indexPivot )
2 int pivot = a[indexPivot ];
3 swap(a[indexPivot],a[R]);// put the pivot in the e
4 int storeIndex = L; // store the right position of
5
6 for(int i = L; i = R-1; i++){
7 if(a[i]  pivot ){
8 swap(a[storeIndex],a[i]);
9 storeIndex ++;
0 }
1 }
2 swap(a[storeIndex],a[R]); // put the pivot in the
3
4 return storeIndex;
5 }
33 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Heap sort
Sort a list A[1..N] in nondecreasing order
1 Build a heap out of A[1..N]
2 Remove the largest element and put it in the Nth position of the list
3 Reconstruct the heap out of A[1..N − 1]
4 Remove the largest element and put it in the N − 1th position of the
list
5 ...
34 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Heap sort - Heap structure
Shape property: Complete binary tree with level L
Heap property: each node is greater than or equal to each of its
children (max-heap)
10
9 7
4 5 6 1
3 2 1 2 3 4 5 6 7 8 9
10 9 7 4 5 6 1 3 2
35 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Heap sort
Heap corresponding to a list A[1..N]
I Root of the tree is A[1]
I Left child of node A[i] is A[2 ∗ i]
I Right child of node A[i] is A[2 ∗ i + 1]
I Height is logN + 1
Operations
I Build-Max-Heap: construct a heap from the original list
I Max-Heapify: repair the following binary tree so that it becomes
Max-Heap
F A tree with root A[i]
F A[i]  max(A[2 ∗ i], A[2 ∗ i + 1]): heap property is not hold
F Subtrees rooted at A[2 ∗ i] and A[2 ∗ i + 1] are Max-Heap
36 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Heap sort
1 void heapify(int a[], int i, int n){
2 // array to be heapified is a[i..n]
3 int L = 2*i;
4 int R = 2*i+1;
5 int max = i;
6 if(L = n  a[L]  a[i])
7 max = L;
8 if(R = n  a[R]  a[max])
9 max = R;
0 if(max != i){
1 swap(a[i],a[max ]);
2 heapify(a,max ,n);
3 }
4 }
37 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Heap sort
1 void buildHeap(int a[], int n){
2 // array is a[1..n]
3 for(int i = n/2; i = 1; i--){
4 heapify(a,i,n);
5 }
6 }
7
8 void heap_Sort(int a[], int n){
9 // array is a[1..n]
0 buildHeap(a,n);
1 for(int i = n; i  1; i--){
2 swap(a[1],a[i]);
3 heapify(a,1,i -1);
4 }
5 }
38 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Data structures
List, Stack, Queue
Graphs
Trees
39 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
List
Collection of objects which are arranged in a linear order
Array
I Continuous allocation
I Accessing elements via indices
Linked List
I Elements are not necessarily allocated continuously
I User pointer to link an element with its successor
I Accessing elements via pointers
40 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Stacks
An ordered list in which all insertions and deletions are made at one
end (called top)
Principle: the last element inserted into the stack must be the first
one to be removed (Last-In-First-Out)
Operations
I Push(x, S): push an element x into the stack S
I Pop(S): remove an element from the stack S, and return this element
I Top(S): return the element at the top of the stack S
I Empty(S): return true if the stack S is empty
41 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Queues
An ordered list in which the insertions are made at one end (called
tail) and the deletions are made at the other end (called head)
Principle: the first element inserted into the queue must be the first
one to be removed (First-In-First-Out)
Applications: items do not have to be processed immediately but they
have to be processed in FIFO order
I Data packets are stored in a queue before being transmitted over the
internet
I Data is transferred asynchronously between two processes: IO buffered,
piples, etc.
I Printer queues, keystroke queues (as we type at the keyboard), etc.
42 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Queues
Operations
I Enqueue(x, Q): push an element x into the queue Q
I Dequeue(Q): remove an element from the queue Q, and return this
element
I Head(Q): return the element at the head of the queue Q
I Tail(Q): return the element at the tail of the queue Q
I Empty(Q): return true if the queue Q is empty
43 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Java Libraries
List
I ArrayList (dynamic array): get(int index), size(), remove(int index),
add(int index, Object o), indexOf(Object o)
I LinkedList (doubly linked list): remove, poll, element, peek, add, offer,
size
Stack
I push, pop, size
Queue
I LinkedList
I remove, poll, element, peek, add, offer, size
Set
I Collection of items
I Methods: add, size, contains
Map
I Map an object (key) to another object (value)
I Methods: put, get, keySet
44 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Examples
1 package week2;
2
3 import java.util.ArrayList;
4
5 public class ExampleArrayList {
6
7 public ExampleArrayList (){
8 ArrayList Integer  L = new ArrayList ();
9 for(int i = 1; i = 10; i++)
0 L.add(i);
1 for(int i = 0; i  L.size (); i++){
2 int item = L.get(i);
3 System.out.print(item +  );
4 }
5 System.out.println(size of L is  + L.size ());
6 }
7 public static void main(String [] args) {
8 ExampleArrayList EAL = new ExampleArrayList ();
9 }
0 }
45 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Examples
1 package week2;
2 import java.util.HashSet;
3 public class ExampleSet {
4
5 public void test (){
6 HashSet Integer  S = new HashSet ();
7 for(int i = 1; i = 10; i ++)
8 S.add(i);
9 for(int i: S){
0 System.out.print(i +  );
1 }
2 System.out.println(S.size () =  + S.size ());
3 System.out.println(S.contains (20));
4 }
5 public static void main(String [] args) {
6 ExampleSet ES = new ExampleSet ();
7 ES.test ();
8 }
9 }
46 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Examples
1 package week2;
2 import java.util.LinkedList;
3 import java.util.Queue;
4 public class ExampleQueue {
5 public static void main(String [] args) {
6 Queue Q = new LinkedList ();
7 /*
8 * Q.element (): return the head of the queue without removing it.
9 * If Q is empty , then raise exception
0 * Q.peek (): return the head of the queue without removing it.
1 * If Q is empty , then return null
2 * Q.remove (): remove and return the head of the queue.
3 * If Q is empty , then raise exception
4 * Q.poll (): remove and return the head of the queue.
5 * If Q is empty , then return null
6 * Q.add(e): add an element to the tail of Q.
7 * If no space available , then raise exception
8 * Q.offer(e): add an element to the tail of Q. If no space availa
9 * then return false. Otherwise , return true
0 */
1 for(int i = 1; i = 10; i++) Q.offer(i);
2 while(Q.size ()  0){
3 int x = (int)Q.remove ();
4 System.out.println(Remove  + x + , head of Q is  + Q.peek ())
5 } 47 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Examples
1 package week2;
2
3 import java.util .*;
4 public class ExampleStack {
5 public ExampleStack (){
6 /*
7 * S.push(e): push an element to the stack
8 * S.pop: remove the element at the top of the stack and return it
9 */
0 Stack S = new Stack ();
1 for(int i = 1; i = 10; i++)
2 S.push(i + 000);
3 while(S.size ()  0){
4 String x = (String)S.pop ();
5 System.out.println(x);
6 }
7 }
8 public static void main(String [] args) {
9 ExampleStack S = new ExampleStack ();
0 }
1 }
48 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Examples
1 package week2;
2
3 import java.util.HashMap;
4 public class ExampleHashMap {
5
6 public ExampleHashMap (){
7 HashMap String , Integer  m = new HashMap String , Integer ();
8 m.put(abc ,1);
9 m.put(def, 1000);
0 m.put(xyz, 100000);
1 for(String k: m.keySet ()){
2 System.out.println(key =  + k +  map to  + m.get(k));
3 }
4 }
5 public static void main(String [] args) {
6 ExampleHashMap EHM = new ExampleHashMap ();
7 }
8 }
49 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Examples
1 package week2;
2 import java.util.Scanner;
3 import java.util.HashMap;
4 import java.io.File;
5 public class CountWords {
6 public CountWords(String filename ){
7 HashMap String , Integer  count = new HashMap String , Integer ();
8 try{
9 Scanner in = new Scanner(new File(filename ));
0 while(in.hasNext ()){
1 String s = in.next ();
2 if(count.get(s) == null)
3 count.put(s, 0);
4 count.put(s, count.get(s) + 1);
5 }
6 for(String w: count.keySet ())
7 System.out.println(Word  + w +  appears  + count.get(w) +
8 in.close ();
9 }catch(Exception ex){
0 ex. printStackTrace ();
1 }
2 }
3 public static void main(String [] args) {
4 CountWords CW = new CountWords(data  week2  CountWords.txt);
5 } 50 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Checking parentheses expression
1 private boolean match(char c, char cc){
2 if(c == ’(’  cc == ’)’) return true;
3 if(c == ’[’  cc == ’]’) return true;
4 if(c == ’{’  cc == ’}’) return true;
5 return false;
6 }
7 public boolean check(String expr ){
8 Stack S = new Stack ();
9 for(int i = 0; i  expr.length (); i++){
0 char c = expr.charAt(i);
1 if(c == ’(’ || c == ’{’|| c == ’[’)
2 S.push(c);
3 else{
4 if(S.size () == 0) return false;
5 char cc = (char)S.pop ();
6 if(! match(cc ,c)) return false;
7 }
8 }
9 return S.size () == 0;
0 }
51 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Water Jug Problem
There are two jugs, a a-gallon one and a b-gallon one (a, b are positive
integer). There is a pump with unlimited water. Neither jug has any
measuring marking on it. How can you get exactly c-gallon jug (c is a
positive integer)?
52 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Water Jug Problem
Search problem
State (x, y): quantity of water in two jugs
Neighboring states
I (x, 0)
I (0, y)
I (a, y)
I (x, b)
I (a, x + y − a) if x + y = a
I (x + y, 0) if x + y  a
I (x + y − b, b) if x + y = b
I (0, x + y) if x + y  b
Final states: (c, y) or (x, c)
53 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Water Jug Problem
Algorithm 1: WaterJug(a, b, c)
Q ← ∅;
Enqueue((a, b), Q);
while Q is not empty do
(x, y) ← Dequeue(Q);
foreach (x0, y0) ∈ neighboring states of (x, y) do
if (x0 = c ∨ y0 = c) then
Solution();
BREAK;
else
Enqueue((x0, y0), Q);
54 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Graphs and Trees
Many objects in our daily lives can be modelled by graphs
I Internets, social networks (facebook), transportation networks,
biological networks, etc.
An graph G is a mathematical object consisting two finites sets,
G = (V , E)
I V is the set of vertices
I E is the set of edges connecting these vertices
Graphs have many types: directed, undirected, multigraphs, etc.
55 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Definitions
An undirected graph G = (V , E)
I V = (v1, v2, . . . , vn) is the set of vertices or nodes
I E ⊆ V × V is the set of edges (also called undirected edges). E is the
set of unordered pair (u, v) such that u 6= v ∈ V
I (u, v) ∈ E iff (v, u) ∈ E
56 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Definitions
A directed graph G = (V , E)
I V = (v1, v2, . . . , vn) is the set of vertices or nodes
I E ⊆ V × V is the set of arcs (also called directed edges). E is the set
of ordered pair (u, v) such that u 6= v ∈ V
57 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Definitions
Given a graph G = (V , E), for each (u, v) ∈ E, we say u and v are
adjacent
Given an undirected graph G = (V , E)
I degree of a vertex v is the number of edges connecting it:
deg(v) = ]{(u, v) | (u, v) ∈ E}
Given a directed graph G = V , E)
I An incoming arc of a vertex is an arc that enters it
I An outgoing arc of a vertex is an arc that leaves it
I indegree (outdegree) of a vertex v is the number of its incoming
(outgoing) arcs
deg+
(v) = ]{(v, u) | (v, u) ∈ E}, deg−
(v) = ]{(u, v) | (u, v) ∈ E}
58 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Definitions
Theorem
Given an undirected graph G = (V , E), we have
2 × |E| =
X
v∈V
deg(v)
Theorem
Given a directed graph G = (V , E), we have
X
v∈V
deg+
(v) =
X
v∈V
deg−
(v) = |E|
59 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Definition - Paths, cycles
Given a graph G = (V , E), a path from vertex u to vertex v in G is a
sequence hu = x0, x1, . . . , xk = vi in which
(xi , xi+1) ∈ E), ∀i = 0, 1, . . . , k − 1
I u: starting point (node)
I v: terminating point
I k is the length of the path (i.e., number of its edges)
A cycle is a path such that the starting and terminating nodes are the
same
A path (cycle) is called simple if it contains no repeated edges (arcs)
A path (cycle) is called elementary if it contains no repeated nodes
60 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Connectivity
Given an undirected graph G = (V , E). G is called connected if for
any pair (u, v) (u, v ∈ V ), there exists always a path from u to v in G
Given a directed graph G = (V , E), G is called
I weakly connected if the corresponding undirected graph of G (i.e., by
removing orientation on its arcs) is connected
I strongly connected if for any pair (u, v) (u, v ∈ V ), there exists
always a path from u to v in G
Given an undirected graph G = (V , E)
I an edge e is called bridge if removing e from G increases the number
of connected components of G
I a vertex v is called articulation point if removing it from G increases
the number of connected components of G
61 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Connectivity
Theorem
An undirected connected graph G can be oriented (each edge of G is
oriented) to obtain a strongly connected graph iff each edge of G lies on
at least one cycle
62 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Planar graphs - Euler Polyhedron Formula
Theorem
Given a connected planar graph having n vertices, m edges. The number
of regions divided by G is m − n + 2.
63 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Planar graphs - Kuratowski’s theorem
Definition
A subdivision of a graph G is a new graph obtained by replacing some
edges by paths using new vertices, edges (each edge is replaced by a path)
Theorem
Kuratowski A graph G is planar iff it does not contain a subdivision of
K3,3 or K5
64 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Graph representation
Two standard ways to represent a graph G = (V , E)
I Adjacency list
F Appropriate with sparse graphs
F Adj[u] = {v | (u, v) ∈ E}, ∀u ∈ V
I Adjacency matrix
F Appropriate with dense graphs
F A = (aij )n×n such that (suppose V = {1, 2, . . . , n})
aij =
(
1 if (i, j) ∈ E,
0 otherwise
65 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Graph representation
In some cases, we can use incidence matrix to represent a directed
graph G = (V , E)
bij =





−1 if edge j leaves vertex i,
1 if edge j enters vertex i,
0 otherwise
66 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o
Applications of Graphs and Trees
Social netwokrs analysis
Transportation networks
Telecommunication networks
etc.
67 / 68
CuuDuongThanCong.com https://fb.com/tailieudientucntt
u
d
u
o
n
g
t
h
a
n
c
o
n
g
.
c
o

phan-tich-va-thiet-ke-thuat-toan_pham-quang-dung-and-do-phan-thuan_chapter01-introduction-to-data-structures-and-algorithms - [cuuduongthancong.com].pdf

  • 1.
    Introduction to DataStructures and Algorithms Pham Quang Dung and Do Phan Thuan Computer Science Department, SoICT, Hanoi University of Science and Technology. July 8, 2016 1 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 2.
    First example Find thelongest subsequence of a given sequence of numbers Given a sequence s = ha1, . . . , ani a subsequence is s(i, j) = hai , . . . , aj i, 1 ≤ i ≤ j ≤ n weight w(s(i, j)) = j X k=i ak Problem: find the subsequence having largest weight Example sequence: -2, 11, -4, 13, -5, 2 The largest weight subsequence is 11, -4, 13 having weight 20 2 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 3.
    Direct algorithm Scan allpossible subsequences n 2 = n2+n 2 Compute and keep the largest weight subsequence 1 public long algo1(int [] a){ 2 int n = a.length; 3 long max = a[0]; 4 for(int i = 0; i n; i++){ 5 for(int j = i; j n; j++){ 6 int s = 0; 7 for(int k = i; k = j; k++) 8 s = s + a[k]; 9 max = max s ? s : max; 0 } 1 } 2 return max; 3 } 3 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 4.
    Direct algorithm Faster algorithm Observation: Pj k=ia[k] = a[j] + Pj−1 k=i a[k] 1 public long algo2(int [] a){ 2 int n = a.length; 3 long max = a[0]; 4 for(int i = 0; i n; i++){ 5 int s = 0; 6 for(int j = i; j n; j++){ 7 s = s + a[j]; 8 max = max s ? s : max; 9 } 0 } 1 return max; 2 } 4 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 5.
    Recursive algorithm Divide thesequence into 2 subsequences at the middle s = s1 :: s2 The largest subsequence might I be in s1 or I be in s2 or I start at some position of s1 and end at some position of s2 Java code: 1 private long maxSeq(int i, int j){ 2 if(i == j) return a[i]; 3 int m = (i+j)/2; 4 long ml = maxSeq(i,m); 5 long mr = maxSeq(m+1,j); 6 long maxL = maxLeft(i,m); 7 long maxR = maxRight(m+1,j); 8 long maxLR = maxL + maxR; 9 long max = ml mr ? ml : mr; 0 max = max maxLR ? max : maxLR; 1 return max; 2 } 3 public long algo3(int [] a){ 4 int n = a.length; 5 return maxSeq (0,n -1); 6 } 5 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 6.
    Recursive algorithm 1 privatelong maxLeft(int i, int j){ 2 long maxL = a[j]; 3 int s = 0; 4 for(int k = j; k = i; k--){ 5 s += a[k]; 6 maxL = maxL s ? maxL : s; 7 } 8 return maxL; 9 } 0 private long maxRight(int i, int j){ 1 long maxR = a[i]; 2 int s = 0; 3 for(int k = i; k = j; k++){ 4 s += a[k]; 5 maxR = maxR s ? maxR : s; 6 } 7 return maxR; 8 } 6 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 7.
    Dynamic programming General principle Division:divide the initial problem into smaller similar problems (subproblems) Storing solutions to subproblems: store the solution to subproblems into memory Aggregation: establish the solution to the initial problem by aggregating solutions to subproblems stored in the memory 7 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 8.
    Dynamic programming Largest subsequence Division: ILet si be the weight of the largest subsequence of a1, . . . , ai ending at ai Aggregation: I s1 = a1 I si = max{si−1 + ai , ai }, ∀i = 2, . . . , n I Solution to the original problem is max{s1, . . . , sn} Number of basic operations is n (best algorithm) 8 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 9.
    Dynamic programming 1 publiclong algo4(int[] a){ 2 int n = a.length; 3 long max = a[0]; 4 int[] s = new int[n]; 5 s[0] = a[0]; 6 max = s[0]; 7 for(int i = 1; i n; i++){ 8 if(s[i-1] 0) s[i] = s[i-1] + a[i]; 9 else s[i] = a[i]; 0 max = max s[i] ? max : s[i]; 1 } 2 return max; 3 } 9 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 10.
    Analyzing algorithms Resources (memory,bandwithd, CPU, etc.) required by the algorithms Most concern is the computational time Input size: number of items in the input Running time: measured in term of the number of primitive operations performed 10 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 11.
    Analyzing algorithms algo1: T(n)= n3 6 + n2 2 + n 3 algo2: T(n) = n2 2 + n 2 algo3: I Count the number of addition (”+”) operation T(n) T(n) = ( 0 if n = 1 T(n 2 ) + T(n 2 ) + n if n 1 I By induction: T(n) = n log2n algo4: T(n) = n 11 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 12.
    Analyzing algorithms Worst-case runningtime: the longest running time for any input of size n Best-case running time: the shortest running time for any input of size n Average-case running time: probabilistic analysis (make assumption of a distribution of the input) yields expected running time 12 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 13.
    Order of growth Consideronly leading term of the function Ignore constant coefficient Example I an3 + bn2 + cn + d = Θ(n3 ) 13 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 14.
    Asymptotic notations Given afucntion g(n), we denote: I Θ(g(n)) = {f (n) : ∃c1, c2, n0 s.t. 0 ≤ c1g(n) ≤ f (n) ≤ c2g(n), ∀n ≥ n0} I O(g(n)) = {f (n) : ∃c, n0 0 s.t. f (n) ≤ cg(n), ∀n ≥ n0} I Ω(g(n)) = {f (n) : ∃c, n0 0 s.t. cg(n) ≤ f (n), ∀n ≥ n0} Examples I 10n2 − 3n = Θ(n2 ) I 10n2 − 3n = O(n3 ) I 10n2 − 3n = Ω(n) 14 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 15.
    Analysis of algorithms Experimentsstudies Write a program implementing the algorithm Execute the program on a machine with different input sizes Measure the actual execution times Plot the results 15 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 16.
    Analysis of algorithms Shortcomingsof experiments studies Need to implement the algorithm, sometime difficult Results may not indicate the running time of other input not experimented To compare two algorithms, it is required to use the same hardware and software environments. 16 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 17.
    Analysis of algorithms Asymptoticalgorithm analysis Use high-level description of the algorithm (pseudo code) Determine the running time of an algorithm as a function of the input size Express this function with asymptotic notations 17 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 18.
    Analysis of algorithms Sequentialstructure: P and Q are two segments of the algorithm (the sequence P; Q) I Time(P; Q) = Time(P) + Time(Q) or I Time(P; Q) = Θ(max(Time(P), Time(Q))) for loop: for i = 1 to m do P(i) I t(i) is the time complexity of P(i) I time complexity of the for loop is Pm i=1 t(i) 18 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 19.
    Analysis of algorithms while(repeat) loop Specify a function of variables of the loop such that this function reduces during the loop To evaluate the running time, we analyze how the function reduces during the loop 19 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 20.
    Analysis of algorithms Example:binary search Function BinarySearch(T[1..n], x) begin i ← 1; j ← n; while i j do k ← (i + j)/2; case x T[k]: j ← k − 1; x = T[k]: i ← k; j ← k; exit; x T[k]: i ← k + 1; endcase endwhile end 20 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 21.
    Analysis of algorithms Example:binary search Denote d = j − i + 1 (number of elements of the array to be investigated) i∗, j∗, d∗ respectively the values of i, j, d after a loop We have If x T[k] then i∗ = i, j∗ = (i + j)/2 − 1, d∗ = j∗ − i∗ + 1 ≤ d/2 If x T[k] then j∗ = j, i∗ = (i + j)/2 + 1, d∗ = j∗ − i∗ + 1 ≤ d/2 Ifx = T[k] then d∗ = 1 Hence, the number of iterations of the loop is dlogne 21 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 22.
    Master theorem T(n) =aT(n/b) + cnk with a ≥ 1, b 1, c 0 are constant If a bk, then T(n) = Θ(nlogba) If a = bk, then T(n) = Θ(nklogn) with logn = log2n If a bk, then T(n) = Θ(nk) Example T(n) = 3T(n/4) + cn2 ⇒ T(n) = Θ(n2) T(n) = 2T(n/2) + n0.5 ⇒ T(n) = Θ(n) T(n) = 16T(n/4) + n ⇒ T(n) = Θ(n2) T(n) = T(3n/7) + 1 ⇒ T(n) = Θ(logn) 22 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 23.
    Sorting Put elements ofa list in a certain order Designing efficient sorting algorithms is very important for other algorithms (search, merge, etc.) Each object is associated with a key and sorting algorithms work on these keys. Two basic operations that used mostly by sorting algorithms I Swap(a, b): swap the values of variables a and b I Compare(a, b): return F true if a is before b in the considered order F false, otherwise. Without loss of generality, suppose we need to sort a list of numbers in nondecreasing order 23 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 24.
    A sorting algorithmis called in-place if the size of additional memory required by the algorithm is O(1) (which does not depend on the size of the input array) A sorting algorithm is called stable if it maintains the relative order of elements with equal keys A sorting algorithm uses only comparison for deciding the order between two elements is called Comparison-based sorting algorithm 24 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 25.
    Insertion Sort At iterationk, put the kth element of the original list in the right order of the sorted list of the first k elements (∀k = 1, . . . , n) Result: after kth iteration, we have a sorted list of the first kth elements of the original list 1 void insertion_sort (int a[], int n){ 2 int k; 3 for(k = 2; k = n; k++){ 4 int last = a[k]; 5 int j = k; 6 while(j 1 a[j-1] last ){ 7 a[j] = a[j -1]; 8 j--; 9 } 0 a[j] = last; 1 } 2 } 25 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 26.
    Selection Sort Put thesmallest element of the original list in the first position Put the second smallest element of the original list in the second position Put the third smallest element of the original list in the third position ... 1 void selection_sort (int a[], int n){ 2 for(int k = 1; k = n; k++){ 3 int min = k; 4 for(int i = k+1; i = n; i++) 5 if(a[min] a[i]) 6 min = i; 7 swap(a[k],a[min ]); 8 } 9 } 26 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 27.
    Bubble sort Pass fromthe beginning of the list: compare and swap two adjacent elements if they are not in the right order Repeat the pass until no swaps are needed 1 void bubble_sort(int a[], int n){ 2 int swapped; 3 do{ 4 swapped = 0; 5 for(int i = 1; i n; i++) 6 if(a[i] a[i+1]){ 7 swap(a[i],a[i+1]); 8 swapped = 1; 9 } 0 }while(swapped == 1); 1 } 27 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 28.
    Merge sort Divide-and-conquer Divide theoriginal list of n/2 into two lists of n/2 elements Recursively merge sort these two lists Merge the two sorted lists 28 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 29.
    Merge sort 1 voidmerge(int a[], int L, int M, int R){ 2 // merge two sorted list a[L..M] and a[M+1..R] 3 int i = L;// first position of the first list a[L. 4 int j = M+1;// first position of the second list a 5 for(int k = L; k = R;k++){ 6 if(i M){// the first list is all scanned 7 TA[k] = a[j]; j++; 8 }else if(j R){// the second list is all scanne 9 TA[k] = a[i]; i++; 0 }else{ 1 if(a[i] a[j]){ 2 TA[k] = a[i]; i++; 3 }else{ 4 TA[k] = a[j]; j++; 5 } 6 } 7 } 8 for(int k = L; k = R; k++) 29 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 30.
    Merge sort 1 voidmerge_sort(int a[], int L, int R){ 2 if(L R){ 3 int M = (L+R)/2; 4 merge_sort(a,L,M); 5 merge_sort(a,M+1,R); 6 merge(a,L,M,R); 7 } 8 } 30 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 31.
    Quick sort Pick anelement, called a pivot, from the original list Rearrange the list so that: I All elements less than pivot come before the pivot I All elements greater or equal to to pivot come after pivot Here, pivot is in the right position in the final sorted list (it is fixed) Recursively sort the sub-list before pivot and the sub-list after pivot 31 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 32.
    Quick sort 1 voidquick_sort(int a[], int L, int R){ 2 if(L R){ 3 int index = (L+R)/2; 4 index = partition(a,L,R,index ); 5 if(L index) 6 quick_sort(a,L,index -1); 7 if(index R) 8 quick_sort(a,index +1,R); 9 } 0 } 32 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 33.
    Quick sort 1 intpartition(int a[], int L, int R, int indexPivot ) 2 int pivot = a[indexPivot ]; 3 swap(a[indexPivot],a[R]);// put the pivot in the e 4 int storeIndex = L; // store the right position of 5 6 for(int i = L; i = R-1; i++){ 7 if(a[i] pivot ){ 8 swap(a[storeIndex],a[i]); 9 storeIndex ++; 0 } 1 } 2 swap(a[storeIndex],a[R]); // put the pivot in the 3 4 return storeIndex; 5 } 33 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 34.
    Heap sort Sort alist A[1..N] in nondecreasing order 1 Build a heap out of A[1..N] 2 Remove the largest element and put it in the Nth position of the list 3 Reconstruct the heap out of A[1..N − 1] 4 Remove the largest element and put it in the N − 1th position of the list 5 ... 34 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 35.
    Heap sort -Heap structure Shape property: Complete binary tree with level L Heap property: each node is greater than or equal to each of its children (max-heap) 10 9 7 4 5 6 1 3 2 1 2 3 4 5 6 7 8 9 10 9 7 4 5 6 1 3 2 35 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 36.
    Heap sort Heap correspondingto a list A[1..N] I Root of the tree is A[1] I Left child of node A[i] is A[2 ∗ i] I Right child of node A[i] is A[2 ∗ i + 1] I Height is logN + 1 Operations I Build-Max-Heap: construct a heap from the original list I Max-Heapify: repair the following binary tree so that it becomes Max-Heap F A tree with root A[i] F A[i] max(A[2 ∗ i], A[2 ∗ i + 1]): heap property is not hold F Subtrees rooted at A[2 ∗ i] and A[2 ∗ i + 1] are Max-Heap 36 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 37.
    Heap sort 1 voidheapify(int a[], int i, int n){ 2 // array to be heapified is a[i..n] 3 int L = 2*i; 4 int R = 2*i+1; 5 int max = i; 6 if(L = n a[L] a[i]) 7 max = L; 8 if(R = n a[R] a[max]) 9 max = R; 0 if(max != i){ 1 swap(a[i],a[max ]); 2 heapify(a,max ,n); 3 } 4 } 37 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 38.
    Heap sort 1 voidbuildHeap(int a[], int n){ 2 // array is a[1..n] 3 for(int i = n/2; i = 1; i--){ 4 heapify(a,i,n); 5 } 6 } 7 8 void heap_Sort(int a[], int n){ 9 // array is a[1..n] 0 buildHeap(a,n); 1 for(int i = n; i 1; i--){ 2 swap(a[1],a[i]); 3 heapify(a,1,i -1); 4 } 5 } 38 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 39.
    Data structures List, Stack,Queue Graphs Trees 39 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 40.
    List Collection of objectswhich are arranged in a linear order Array I Continuous allocation I Accessing elements via indices Linked List I Elements are not necessarily allocated continuously I User pointer to link an element with its successor I Accessing elements via pointers 40 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 41.
    Stacks An ordered listin which all insertions and deletions are made at one end (called top) Principle: the last element inserted into the stack must be the first one to be removed (Last-In-First-Out) Operations I Push(x, S): push an element x into the stack S I Pop(S): remove an element from the stack S, and return this element I Top(S): return the element at the top of the stack S I Empty(S): return true if the stack S is empty 41 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 42.
    Queues An ordered listin which the insertions are made at one end (called tail) and the deletions are made at the other end (called head) Principle: the first element inserted into the queue must be the first one to be removed (First-In-First-Out) Applications: items do not have to be processed immediately but they have to be processed in FIFO order I Data packets are stored in a queue before being transmitted over the internet I Data is transferred asynchronously between two processes: IO buffered, piples, etc. I Printer queues, keystroke queues (as we type at the keyboard), etc. 42 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 43.
    Queues Operations I Enqueue(x, Q):push an element x into the queue Q I Dequeue(Q): remove an element from the queue Q, and return this element I Head(Q): return the element at the head of the queue Q I Tail(Q): return the element at the tail of the queue Q I Empty(Q): return true if the queue Q is empty 43 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 44.
    Java Libraries List I ArrayList(dynamic array): get(int index), size(), remove(int index), add(int index, Object o), indexOf(Object o) I LinkedList (doubly linked list): remove, poll, element, peek, add, offer, size Stack I push, pop, size Queue I LinkedList I remove, poll, element, peek, add, offer, size Set I Collection of items I Methods: add, size, contains Map I Map an object (key) to another object (value) I Methods: put, get, keySet 44 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 45.
    Examples 1 package week2; 2 3import java.util.ArrayList; 4 5 public class ExampleArrayList { 6 7 public ExampleArrayList (){ 8 ArrayList Integer L = new ArrayList (); 9 for(int i = 1; i = 10; i++) 0 L.add(i); 1 for(int i = 0; i L.size (); i++){ 2 int item = L.get(i); 3 System.out.print(item + ); 4 } 5 System.out.println(size of L is + L.size ()); 6 } 7 public static void main(String [] args) { 8 ExampleArrayList EAL = new ExampleArrayList (); 9 } 0 } 45 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 46.
    Examples 1 package week2; 2import java.util.HashSet; 3 public class ExampleSet { 4 5 public void test (){ 6 HashSet Integer S = new HashSet (); 7 for(int i = 1; i = 10; i ++) 8 S.add(i); 9 for(int i: S){ 0 System.out.print(i + ); 1 } 2 System.out.println(S.size () = + S.size ()); 3 System.out.println(S.contains (20)); 4 } 5 public static void main(String [] args) { 6 ExampleSet ES = new ExampleSet (); 7 ES.test (); 8 } 9 } 46 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 47.
    Examples 1 package week2; 2import java.util.LinkedList; 3 import java.util.Queue; 4 public class ExampleQueue { 5 public static void main(String [] args) { 6 Queue Q = new LinkedList (); 7 /* 8 * Q.element (): return the head of the queue without removing it. 9 * If Q is empty , then raise exception 0 * Q.peek (): return the head of the queue without removing it. 1 * If Q is empty , then return null 2 * Q.remove (): remove and return the head of the queue. 3 * If Q is empty , then raise exception 4 * Q.poll (): remove and return the head of the queue. 5 * If Q is empty , then return null 6 * Q.add(e): add an element to the tail of Q. 7 * If no space available , then raise exception 8 * Q.offer(e): add an element to the tail of Q. If no space availa 9 * then return false. Otherwise , return true 0 */ 1 for(int i = 1; i = 10; i++) Q.offer(i); 2 while(Q.size () 0){ 3 int x = (int)Q.remove (); 4 System.out.println(Remove + x + , head of Q is + Q.peek ()) 5 } 47 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 48.
    Examples 1 package week2; 2 3import java.util .*; 4 public class ExampleStack { 5 public ExampleStack (){ 6 /* 7 * S.push(e): push an element to the stack 8 * S.pop: remove the element at the top of the stack and return it 9 */ 0 Stack S = new Stack (); 1 for(int i = 1; i = 10; i++) 2 S.push(i + 000); 3 while(S.size () 0){ 4 String x = (String)S.pop (); 5 System.out.println(x); 6 } 7 } 8 public static void main(String [] args) { 9 ExampleStack S = new ExampleStack (); 0 } 1 } 48 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 49.
    Examples 1 package week2; 2 3import java.util.HashMap; 4 public class ExampleHashMap { 5 6 public ExampleHashMap (){ 7 HashMap String , Integer m = new HashMap String , Integer (); 8 m.put(abc ,1); 9 m.put(def, 1000); 0 m.put(xyz, 100000); 1 for(String k: m.keySet ()){ 2 System.out.println(key = + k + map to + m.get(k)); 3 } 4 } 5 public static void main(String [] args) { 6 ExampleHashMap EHM = new ExampleHashMap (); 7 } 8 } 49 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 50.
    Examples 1 package week2; 2import java.util.Scanner; 3 import java.util.HashMap; 4 import java.io.File; 5 public class CountWords { 6 public CountWords(String filename ){ 7 HashMap String , Integer count = new HashMap String , Integer (); 8 try{ 9 Scanner in = new Scanner(new File(filename )); 0 while(in.hasNext ()){ 1 String s = in.next (); 2 if(count.get(s) == null) 3 count.put(s, 0); 4 count.put(s, count.get(s) + 1); 5 } 6 for(String w: count.keySet ()) 7 System.out.println(Word + w + appears + count.get(w) + 8 in.close (); 9 }catch(Exception ex){ 0 ex. printStackTrace (); 1 } 2 } 3 public static void main(String [] args) { 4 CountWords CW = new CountWords(data week2 CountWords.txt); 5 } 50 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 51.
    Checking parentheses expression 1private boolean match(char c, char cc){ 2 if(c == ’(’ cc == ’)’) return true; 3 if(c == ’[’ cc == ’]’) return true; 4 if(c == ’{’ cc == ’}’) return true; 5 return false; 6 } 7 public boolean check(String expr ){ 8 Stack S = new Stack (); 9 for(int i = 0; i expr.length (); i++){ 0 char c = expr.charAt(i); 1 if(c == ’(’ || c == ’{’|| c == ’[’) 2 S.push(c); 3 else{ 4 if(S.size () == 0) return false; 5 char cc = (char)S.pop (); 6 if(! match(cc ,c)) return false; 7 } 8 } 9 return S.size () == 0; 0 } 51 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 52.
    Water Jug Problem Thereare two jugs, a a-gallon one and a b-gallon one (a, b are positive integer). There is a pump with unlimited water. Neither jug has any measuring marking on it. How can you get exactly c-gallon jug (c is a positive integer)? 52 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 53.
    Water Jug Problem Searchproblem State (x, y): quantity of water in two jugs Neighboring states I (x, 0) I (0, y) I (a, y) I (x, b) I (a, x + y − a) if x + y = a I (x + y, 0) if x + y a I (x + y − b, b) if x + y = b I (0, x + y) if x + y b Final states: (c, y) or (x, c) 53 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 54.
    Water Jug Problem Algorithm1: WaterJug(a, b, c) Q ← ∅; Enqueue((a, b), Q); while Q is not empty do (x, y) ← Dequeue(Q); foreach (x0, y0) ∈ neighboring states of (x, y) do if (x0 = c ∨ y0 = c) then Solution(); BREAK; else Enqueue((x0, y0), Q); 54 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 55.
    Graphs and Trees Manyobjects in our daily lives can be modelled by graphs I Internets, social networks (facebook), transportation networks, biological networks, etc. An graph G is a mathematical object consisting two finites sets, G = (V , E) I V is the set of vertices I E is the set of edges connecting these vertices Graphs have many types: directed, undirected, multigraphs, etc. 55 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 56.
    Definitions An undirected graphG = (V , E) I V = (v1, v2, . . . , vn) is the set of vertices or nodes I E ⊆ V × V is the set of edges (also called undirected edges). E is the set of unordered pair (u, v) such that u 6= v ∈ V I (u, v) ∈ E iff (v, u) ∈ E 56 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 57.
    Definitions A directed graphG = (V , E) I V = (v1, v2, . . . , vn) is the set of vertices or nodes I E ⊆ V × V is the set of arcs (also called directed edges). E is the set of ordered pair (u, v) such that u 6= v ∈ V 57 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 58.
    Definitions Given a graphG = (V , E), for each (u, v) ∈ E, we say u and v are adjacent Given an undirected graph G = (V , E) I degree of a vertex v is the number of edges connecting it: deg(v) = ]{(u, v) | (u, v) ∈ E} Given a directed graph G = V , E) I An incoming arc of a vertex is an arc that enters it I An outgoing arc of a vertex is an arc that leaves it I indegree (outdegree) of a vertex v is the number of its incoming (outgoing) arcs deg+ (v) = ]{(v, u) | (v, u) ∈ E}, deg− (v) = ]{(u, v) | (u, v) ∈ E} 58 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 59.
    Definitions Theorem Given an undirectedgraph G = (V , E), we have 2 × |E| = X v∈V deg(v) Theorem Given a directed graph G = (V , E), we have X v∈V deg+ (v) = X v∈V deg− (v) = |E| 59 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 60.
    Definition - Paths,cycles Given a graph G = (V , E), a path from vertex u to vertex v in G is a sequence hu = x0, x1, . . . , xk = vi in which (xi , xi+1) ∈ E), ∀i = 0, 1, . . . , k − 1 I u: starting point (node) I v: terminating point I k is the length of the path (i.e., number of its edges) A cycle is a path such that the starting and terminating nodes are the same A path (cycle) is called simple if it contains no repeated edges (arcs) A path (cycle) is called elementary if it contains no repeated nodes 60 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 61.
    Connectivity Given an undirectedgraph G = (V , E). G is called connected if for any pair (u, v) (u, v ∈ V ), there exists always a path from u to v in G Given a directed graph G = (V , E), G is called I weakly connected if the corresponding undirected graph of G (i.e., by removing orientation on its arcs) is connected I strongly connected if for any pair (u, v) (u, v ∈ V ), there exists always a path from u to v in G Given an undirected graph G = (V , E) I an edge e is called bridge if removing e from G increases the number of connected components of G I a vertex v is called articulation point if removing it from G increases the number of connected components of G 61 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 62.
    Connectivity Theorem An undirected connectedgraph G can be oriented (each edge of G is oriented) to obtain a strongly connected graph iff each edge of G lies on at least one cycle 62 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 63.
    Planar graphs -Euler Polyhedron Formula Theorem Given a connected planar graph having n vertices, m edges. The number of regions divided by G is m − n + 2. 63 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 64.
    Planar graphs -Kuratowski’s theorem Definition A subdivision of a graph G is a new graph obtained by replacing some edges by paths using new vertices, edges (each edge is replaced by a path) Theorem Kuratowski A graph G is planar iff it does not contain a subdivision of K3,3 or K5 64 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 65.
    Graph representation Two standardways to represent a graph G = (V , E) I Adjacency list F Appropriate with sparse graphs F Adj[u] = {v | (u, v) ∈ E}, ∀u ∈ V I Adjacency matrix F Appropriate with dense graphs F A = (aij )n×n such that (suppose V = {1, 2, . . . , n}) aij = ( 1 if (i, j) ∈ E, 0 otherwise 65 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 66.
    Graph representation In somecases, we can use incidence matrix to represent a directed graph G = (V , E) bij =      −1 if edge j leaves vertex i, 1 if edge j enters vertex i, 0 otherwise 66 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o
  • 67.
    Applications of Graphsand Trees Social netwokrs analysis Transportation networks Telecommunication networks etc. 67 / 68 CuuDuongThanCong.com https://fb.com/tailieudientucntt u d u o n g t h a n c o n g . c o