Part1 Array
Part1 Array
1
1D Array Representation In C++
Memory
a b c d
start
• location(x[i]) = start + i
2
Space Overhead
Memory
a b c d
start
5
Columns Of A 2D Array
6
2D Array Representation In C++
2-dimensional array x
a, b, c, d
e, f, g, h
i, j, k, l
view 2D array as a 1D array of rows
x = [row0, row1, row 2]
row 0 = [a,b, c, d]
row 1 = [e, f, g, h]
row 2 = [i, j, k, l]
and store as 4 1D arrays
7
2D Array Representation In C++
x[]
a b c d
e f g h
i j k l
8
Space Overhead
x[]
a b c d
e f g h
i j k l
9
Array Representation In C++
x[]
a b c d
e f g h
i j k l
10
Row-Major Mapping
• Example 3 x 4 array:
abcd
efgh
i jkl
• Convert into 1D array y by collecting elements by
rows.
• Within a row elements are collected from left to right.
• Rows are collected from top to bottom.
• We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l}
row 0 row 1 row 2 … row i 11
Locating Element x[i][j]
0 c 2c 3c ic
13
Disadvantage
14
Column-Major Mapping
abcd
efgh
i jkl
• Convert into 1D array y by collecting
elements by columns.
• Within a column elements are collected
from top to bottom.
• Columns are collected from left to right.
• We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
15
Matrix
Table of values. Has rows and columns, but
numbering begins at 1 rather than 0.
a b c d row 1
efgh row 2
i jkl row 3
• Use notation x(i,j) rather than x[i][j].
• May use a 2D array to represent a matrix.
16
Shortcomings Of Using A 2D
Array For A Matrix
18
Diagonal Matrix
1000
0200
0030
0004
19
Lower Triangular Matrix
An n x n matrix in which all nonzero terms are
either on or below the diagonal.
100 0
230 0
456 0
7 8 9 10
• x(i,j) is part of lower triangle iff i >= j.
• number of elements in lower triangle is 1 + 2 + … + n = n(n+1)/2.
• store only the lower triangle
20
Array Of Arrays Representation
x[]
2 3
4 5 6
7 8 9 l0
0 1 3 6
r 1 r2 r3 … row i
• Linear list.
• One end is called top.
• Other end is called bottom.
• Additions to and removals from the top end
only.
25
Stack Of Cups
top F
top E E
D D
C C
B B
bottom A bottom A
26
Parentheses Matching
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
– Output pairs (u,v) such that the left parenthesis at
position u is matched with the right parenthesis at v.
• (2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38)
• (a+b))*((c+d)
– (0,4)
– right parenthesis at 5 has no matching left
parenthesis
– (8,12)
left parenthesis at 7 has no matching right parenthesis
27
Parentheses Matching
• scan expression from left to right
• when a left parenthesis is encountered, add
its position to the stack
• when a right parenthesis is encountered,
remove matching position from stack
28
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
2
1
0
29
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
15
1
0 (2,6) (1,13)
30
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
21
1
0 (2,6) (1,13) (15,19)
31
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
27
1
0 (2,6) (1,13) (15,19) (21,25)
32
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
34
0 (2,6) (1,13) (15,19) (21,25)(27,31) (0,32)
• and so on 33
Method Invocation And Return
public void a()
{ …; b(); …}
public void b()
{ …; c(); …} return address in d()
public void c() return address in c()
{ …; d(); …} return address in e()
public void d() return address in d()
{ …; e(); …}
return address in c()
return address in b()
public void e()
return address in a()
{ …; c(); …} 34
Towers Of Hanoi/Brahma
4
3
2
1
A B C
• 64 gold disks to be moved from tower A to tower C
• each tower operates as a stack
35
• cannot place big disk on top of a smaller one
Towers Of Hanoi/Brahma
3
2
1
A B C
• 3-disk Towers Of Hanoi/Brahma
36
Towers Of Hanoi/Brahma
2
1 3
A B C
• 3-disk Towers Of Hanoi/Brahma
37
Towers Of Hanoi/Brahma
1 2 3
A B C
• 3-disk Towers Of Hanoi/Brahma
38
Towers Of Hanoi/Brahma
3
1 2
A B C
• 3-disk Towers Of Hanoi/Brahma
39
Towers Of Hanoi/Brahma
3
2 1
A B C
• 3-disk Towers Of Hanoi/Brahma
40
Towers Of Hanoi/Brahma
3 2 1
A B C
• 3-disk Towers Of Hanoi/Brahma
41
Towers Of Hanoi/Brahma
2
3 1
A B C
• 3-disk Towers Of Hanoi/Brahma
42
Towers Of Hanoi/Brahma
3
2
1
A B C
• 3-disk Towers Of Hanoi/Brahma
• 7 disk moves 43
Recursive Solution
A B C
• n > 0 gold disks to be moved from A to C using B
• move top n-1 disks from A to B using C
44
Recursive Solution
A B C
• move top disk from A to C
45
Recursive Solution
A B C
• move top n-1 disks from B to C using A
46
Recursive Solution
B C
A
• moves(n) = 0 when n = 0
• moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0
47
Towers Of Hanoi/Brahma
48
Algorithm
Hanoi(N, Src, Aux, Dst)
if N is 0
exit
else
Hanoi(N - 1, Src, Dst, Aux)
Move from Src to Dst
Hanoi(N - 1, Aux, Src, Dst)
49
S(3, A,B,C)
S(2, A,C,B)
S(1, A,B,C)
S(0, A,C,B); A->C; S(0, B,A,C)
A->B
S(1, C,A,B)
S(0, C,B,A); C->B; S(0, A,C,B)
A-> C
S(2, B,A,C)
S(1, B,C,A)
S(0, B,A,C); B->A; S(0, C,B,A)
B->C
S(1, A,B,C)
S(0, A,C,B); A->C; S(0, B,A,C)
50
Method Invocation And Return
public void a()
{ ..; b();..
return }
public void b()
{ ..; c();.. return}
public void c()
{ ..; d(); ..return}
public void d() return Hanoi(1,A,B,C)
{ …; return} return Hanoi(2,A,C,B)
return Hanoi(3,A,B,C)
51
Rat In A Maze
52
Rat In A Maze
• Move down.
56
Rat In A Maze
• Move left.
57
Rat In A Maze
• Move down.
58
Rat In A Maze
• Move right.
• Backtrack. 61
Rat In A Maze
• Move downward.
62
Rat In A Maze
• Move right.
63
Rat In A Maze
66
Stacks
– Standard operations:
• IsEmpty … return true iff stack is empty
• Top … return top element of stack
• Push … add an element to the top of the stack
• Pop … delete the top element of the stack
67
Stacks
68
Stacks
a b c d e
0 1 2 3 4 5 6
69
Derive From arrayList
a b c d e
0 1 2 3 4 5 6
71
Template Class
#include <iostream>
using namespace std;
return max;
}
72
How to use template?
int main()
{
int int1,int2,int3;
double double1,double2,double3;
cout << "Input three double values:";
cin >> double1 >> double2 >> double3;
cout << "The maximum integer value is: "
<< maximum(double1,double2,double3) << endl;
char char1,char2,char3;
cout << "Input three characters:";
cin >> char1 >> char2 >> char3;
cout << "The maximum character value is: "
<< maximum(char1,char2,char3) << endl;
system("pause");
return 0;
}
73
Constructor
#ifndef POINT_H
#define POINT_H
class Point {
public:Point(); // parameterless default constructor
Point(int new_x, int new_y);
private:
int x;
int y;
};
#endif
74
Implementation
Point::Point() { // default constructor
x = 0;
y = 0; }
75
How to use constructor?
Point p; // calls our default constructor
76
The Class Stack
template<class T>
class Stack
{
public:
Stack(int stackCapacity = 10);
~Stack() {delete [] stack;}
bool IsEmpty() const;
T& Top() const;
void Push(const T& item);
void Pop();
private:
T *stack; // array for stack elements
int top; // position of top element
int capacity; // capacity of stack array
77
};
Constructor
template<class T>
Stack<T>::Stack(int stackCapacity)
:capacity(stackCapacity)
{
if (capacity < 1)
throw “Stack capacity must be > 0”;
stack = new T[capacity];
top = -1;
}
78
IsEmpty
template<class T>
inline bool Stack<T>::IsEmpty() const
{return top == -1}
79
Top
template<class T>
inline T& Stack<T>::Top() const
{
if (IsEmpty())
throw “Stack is empty”;
return stack[top];
}
80
Push
a b c d e
0 1 2 3 4 top
template<class T>
void Stack<T>::Push(const T& x)
{// Add x to the stack.
if (top == capacity - 1)
{ChangeSize1D(stack, capacity,
2*capacity);
capacity *= 2;
}
// add at stack top
stack[++top] = x; 81
}
Pop
a b c d e
0 1 2 3 4 top
void Stack<T>::Pop()
{
if (IsEmpty())
throw “Stack is empty. Cannot delete.”;
stack[top--].~T(); // destructor for T
}
82
Queues
• Linear list.
• One end is called front.
• Other end is called rear.
• Additions are done at the rear only.
• Removals are made from the front only.
83
Bus Stop Queue
Bus
Stop
84
Bus Stop Queue
Bus
Stop
85
Bus Stop Queue
Bus
Stop
front rear
rear
86
Bus Stop Queue
Bus
Stop
front rear
rear
87
Revisit Of Stack Applications
• Applications in which the stack cannot
be replaced with a queue.
– Parentheses matching.
– Towers of Hanoi.
– Method invocation and return.
• Application in which the stack may be
replaced with a queue.
– Rat in a maze.
• Results in finding shortest path to exit.
88
Wire Routing
89
Lee’s Wire Router
start pin
end pin
end pin
1 1
end pin 2 2
1 1 2
2 2
98
Queue in an Array
99
Derive From arrayList
a b c d e
0 1 2 3 4 5 6
100
O(1) Pop and Push
101
Custom Array Queue
• Use a 1D array queue.
queue[]
[2] [3]
[1] [4]
[2] [3]
A B
[1] C [4]
[0] [5]
103
Custom Array Queue
• Another possible configuration with 3 elements.
[2] [3]
[1] C [4]
B A
[0] [5]
104
Custom Array Queue
• Use integer variables front and rear.
– front is one position counterclockwise from
first element
– rear gives position of last element
B A
[0] [5] [0] [5] 105
Push An Element
• Move rear one clockwise.
[2] [3]
A B rear
front
[1] C [4]
[0] [5]
106
Push An Element
• Move rear one clockwise.
• Then put into queue[rear].
[2] [3]
A B
front
[1] C [4]
D
[0] [5]
rear 107
Pop An Element
• Move front one clockwise.
[2] [3]
A B rear
front
[1] C [4]
[0] [5]
108
Pop An Element
• Move front one clockwise.
• Then extract from queue[front].
[1] C [4]
[0] [5]
109
Moving rear Clockwise
• rear++;
if (rear = = capacity) rear = 0;
[2] [3]
A B rear
front
[1] C [4]
[0] [5]
rear
front
[1] C [4]
B A
[0] [5]
111
Empty That Queue
[2] [3]
rear
[1] C [4]
B
[0] [5]
front
112
Empty That Queue
[2] [3]
rear
[1] C [4]
[0] [5]
front
113
Empty That Queue
[2] [3]
rear
[1] [4]
114
A Full Tank Please
[2] [3]
rear
front
[1] C [4]
B A
[0] [5]
115
A Full Tank Please
[2] [3]
rear
D
front
[1] C [4]
B A
[0] [5]
116
A Full Tank Please
[2] [3] rear
D E
front
[1] C [4]
B A
[0] [5]
117
A Full Tank Please
[2] [3]
D E
front
[1] C F [4]
B A rear
[0] [5]
• When a series of adds causes the queue to become full, front = rear.
• So we cannot distinguish between a full queue and an empty queue!
118
Ouch!!!!!
• Remedies.
– Don’t let the queue get full.
• When the addition of an element will cause the queue to
be full, increase array size.
• This is what the text does.
– Define a boolean variable lastOperationIsPush.
• Following each push set this variable to true.
• Following each pop set to false.
• Queue is empty iff (front == rear) && !
lastOperationIsPush
• Queue is full iff (front == rear) && lastOperationIsPush
119
Ouch!!!!!
• Remedies (continued).
– Define an integer variable size.
• Following each push do size++.
• Following each pop do size--.
• Queue is empty iff (size == 0)
• Queue is full iff (size == arrayLength)
– Performance is slightly better when first strategy is
used.
120