week08_StackQueue
week08_StackQueue
STRUCUTURES
AND ALGORITHMS
CONTENT
• Stack
3 4
STACK STACK
Nguồn: https://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29
5 6
IMPLEMENT STACKS WITH SINGLY LINKED LIST IMPLEMENT STACKS WITH SINGLY LINKED LIST
7 8
IMPLEMENT STACKS WITH SINGLY LINKED LIST IMPLEMENT STACKS WITH SINGLY LINKED LIST
9 10
Function call stack: Stack is widely used to manage function calls and local Backtracking Algorithms: In algorithms like depth-first search (DFS) and
variables in programming languages. When a function is called, its context backtracking algorithms (For example, solving puzzles like the N-Queens problem
(including arguments and local variables) is pushed onto the stack. When the or Sudoku), a stack can be used to keeps track of which nodes or states have been
function returns, its context is pushed off the stack, allowing correct nesting of visited. This allows for easy backtracking to explore alternatives when needed.
the function.
Expression Analysis: Stack is used to analyze and understand expressions in
Undo mechanism: Stacks are used in applications that require the Undo feature,
compilers and interpreters. They help maintain operator precedence and evaluate
such as text editors, graphics software, or version management systems. Each
expressions correctly.
user action can be pushed onto the stack, and undoing an action involves taking it
off the stack to revert the change.
11 12
SOME APPLICATIONS OF STACKS CONTENT
13 14
EXERCISE: CHECK THE SYMMETRY OF THE PARENTHESES EXERCISE: CHECK THE SYMMETRY OF THE PARENTHESES
15 16
EXERCISE: CHECK THE SYMMETRY OF THE PARENTHESES EXAMPLE
If S is not empty
If A and B are not symmetrical (opening and closing brackets are of different types), then the conclusion is that E is
not symmetrical
At the end of the review, if S is not empty, the conclusion is that E is not symmetric, otherwise E is symmetric
17 18
EXAMPLE EXAMPLE
19 20
EXAMPLE EXAMPLE
(
[ [
21 22
EXAMPLE EXAMPLE
• Step 1: Consider the next parenthesis as the opening parenthesis “(“ -> put • Step 1: Consider the next parenthesis as the opening parenthesis “(“ -> put the opening
the opening parenthesis on the stack parenthesis on the stack
• Step 2: Consider the next parenthesis as the closing parenthesis “)” -> remove the
• Step 2: Consider the next parenthesis as the closing parenthesis “)” -> remove
opening parenthesis from the stack and match “(“ “)” -> the result is correct so we
the opening parenthesis from the stack and match “(“ “)” -> the result is correct
continue browsing
so we continue browsing
• Step 3: Encounter opening parenthesis “[“ -> put this opening parenthesis on the stack
• Step 3: Encounter opening parenthesis “[“ -> put this opening parenthesis on
• Step 4: Encounter the opening parenthesis “(“ -> put this opening parenthesis on the
the stack stack
• Step 4: Encounter the opening parenthesis “(“ -> put this opening parenthesis • Step 5: Encounter opening parenthesis “{“ -> put this opening parenthesis on the stack
on the stack • Step 6: Encounter a closing parenthesis “}” -> take an opening parenthesis “{“ out of the
{
• Step 5: Encounter opening parenthesis “{“ -> put this opening parenthesis on stack
the stack ( • Matches “{“ “}” -> the result is correct -> continue
(
[ [
23 24
EXAMPLE EXAMPLE
25 26
EXAMPLE EXAMPLE
27 28
EXAMPLE EXAMPLE
29 30
EXAMPLE EXAMPLE
31 32
IMPLEMENTATION IMPLEMENTATION
33 34
IMPLEMENTATION IMPLEMENTATION
int match(char a, int b){ int match(char a, int b){ int check(char* s){
if(a == '(' && b == ')') return 1; if(a == '(' && b == ')') return 1; for(int i = 0; i < strlen(s); i++){
if(a == '{' && b == '}') return 1; if(a == '{' && b == '}') return 1; if(s[i] == '(' || s[i] == '{' || s[i] == '[')
if(a == '[' && b == ']') return 1; if(a == '[' && b == ']') return 1; push(s[i]);
return 0; return 0; else{
} } if(top==NULL) return 0;
char o = pop();
if(!match(o,s[i])) return 0;
}
}
return top == NULL;
}
35 36
IMPLEMENTATION CONTENT
37 38
A queue is a linear data structure with two ends, head Queue Returned
and tail Operation state element
9
The operation of adding new elements is performed in
Front / Head Back / Tail / Rear
Source: https://www.geeksforgeeks.org/queue-data-structure/
39 40
IMPLEMENT A QUEUE WITH A SINGLY LINKED LIST IMPLEMENT A QUEUE WITH A SINGLY LINKED LIST
41 42
43 44
SOME APPLICATIONS OF QUEUES SOME APPLICATIONS OF QUEUES
Web server request handling: Web servers use queues to manage incoming HTTP Task management in multithreading: In multithreaded applications, queues can be
requests. Each incoming request is placed into a queue and processed by threads used to manage tasks that need to be executed concurrently. Execution threads
or workflows, allowing the server to process multiple requests at once. remove tasks from the queue and execute the corresponding work.
Buffering in I/O operations: Queues are used to buffer data during I/O operations. Order fulfillment in warehouses: In logistics and storage management, queues can
For example, when data is read from a file or network drive, it is often placed in a be used to manage the order fulfillment process. Orders are placed into a queue
queue before processing to smooth out variations in the rate at which the data is for selection, packing and delivery to ensure efficient processing.
received.
45 46
CONTENT MAZE
47 7 bước 48
MAZE MAZE
Data: Data:
• Line 1: write 4 positive integers n, m, r, c in which n and m are respectively the number of rows • Line 1: write 4 positive integers n, m, r, c in which n and m are respectively the number of rows
and columns of matrix A (1 <= n,m <= 999) and r, c are the indexes respectively. Row and and columns of matrix A (1 <= n,m <= 999) and r, c are the indexes respectively. Row and
column numbers of the starting cell. column numbers of the starting cell.
• Line i+1 (i=1,...,n): the ith line of matrix A • Line i+1 (i=1,...,n): the ith line of matrix A
• Result: • Result:
• Write the shortest number of steps needed to exit the maze, or write the value -1 if no path • Write the shortest number of steps needed to exit the maze, or write the value -1 if no path
can be found to exit the maze.. can be found to exit the maze..
49 50
MAZE ALGORITHM
The breadth-first search algorithm finds the shortest path on the state transition model:
Example:
stdin stdout Initial state
8 12 5 6 7
110000100001
Target state
Initial state
100011010011
001000000000 Each state s will have a set of N(s) of
100000100101
100100000100
101010001010 neighboring states
000010100000
101101110101
Target state
51 52
ALGORITHM ILLUSTRATION
The breadth-first search algorithm finds the shortest path on the state transition model: 1 2 3 4 5 6 7 8 9 10 11 12
1
2
findPath(s0, N){
3
Init Queue
Queue.PUSH(s0); Initial state 4
visited[s0] = true; 5
while Queue not empty do{ X
s = Queue.POP(); 6
for x N(s) do{
7
if not visited[x] and check(x) then{
if target(x) then Initialize an empty Q queue 8
return x;
else{ Insert state (5,6) into Q
Queue.PUSH(x);
visited[x] = true;
} Target state (5,6)
}
}
}
}
53 54
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Take (5,6) out of Q 8 Take (5,7) out of Q 8
Put the state (5,7), (5, 5), (4, 6), (6,6) into Q Put states (6,7), (5, 8) into Q
(5,6) (5,7) (5,5) (4,6) (6,6) (5,7) (5,5) (4,6) (6,6) (6,7) (5,8)
55 56
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Take (5,5) out of Q 8 Take (4,6) out of Q 8
57 58
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Take (6,6) out of Q 8 Take (6,7) out of Q 8
59 60
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Take (5,8) out of Q 8 Take (4,5) out of Q 8
Insert states (5,9) and (4,8) into Q Insert states (4,4) and (3,5) into Q
(5,8) (4,5) (3,6) (7,6) (6,8) (5,9) (4,8) (4,5) (3,6) (7,6) (6,8) (5,9) (4,8) (4,4) (3,5)
61 62
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Take (3,6) out of Q 8 Take (7,6) out of Q 8
63 64
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Bring the state (7,8) into Q Bring the state (4,9) into Q
(6,8) (5,9) (4,8) (4,4) (3,5) (3,7) (7,8) (5,9) (4,8) (4,4) (3,5) (3,7) (7,8) (4,9)
65 66
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Take (4,8) out of Q 8 Take (4,4) out of Q 8
Bring the state (3,8) into Q Put the state (4,3), (3,4) into Q
(4,8) (4,4) (3,5) (3,7) (7,8) (4,9) (3,8) (4,4) (3,5) (3,7) (7,8) (4,9) (3,8) (4,3) (3,4)
67 68
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Take (3,5) out of Q 8 Take (3,7) out of Q 8
No new states can be added to Q Bring the new state (2,7) into Q
(3,5) (3,7) (7,8) (4,9) (3,8) (4,3) (3,4) (3,7) (7,8) (4,9) (3,8) (4,3) (3,4) (2,7)
69 70
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Take (7,8) out of Q 8 Take (4,9) out of Q 8
Bring new status (7,9) into Q Bring the new state (3,9) into Q
(7,8) (4,9) (3,8) (4,3) (3,4) (2,7) (7,9) (4,9) (3,8) (4,3) (3,4) (2,7) (7,9) (3,9)
71 72
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Take (3,8) out of Q 8 Take (4,3) out of Q 8
No new states can be added to Q Bringing new states (4,2), (5,3) into Q
(3,8) (4,3) (3,4) (2,7) (7,9) (3,9) (4,3) (3,4) (2,7) (7,9) (3,9) (4,2) (5,3)
73 74
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Bring the new state (2,4) into Q No new states can be added to Q
(3,4) (2,7) (7,9) (3,9) (4,2) (5,3) (2,4) (2,7) (7,9) (3,9) (4,2) (5,3) (2,4)
75 76
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
8 Take (3,9) out of Q 8
Take (7,9) out of Q
Bring new states (8,9), (7,10) into Q Bring new states (2,9), (3,10) into Q
(7,9) (3,9) (4,2) (5,3) (2,4) (8,9) (7,10) (3,9) (4,2) (5,3) (2,4) (8,9) (7,10) (2,9) (3,10)
77 78
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
Bring new states (3,2), (5,2) into Q No new states can be added to Q
(4,2) (5,3) (2,4) (8,9) (7,10) (2,9) (3,10) (3,2) (5,2) (5,3) (2,4) (8,9) (7,10) (2,9) (3,10) (3,2) (5,2)
79 80
ILLUSTRATION ILLUSTRATION
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12
1 1
2 2
3 3
4 4
5 X 5 X
6 6
7 7
(2,4) (8,9) (7,10) (2,9) (3,10) (3,2) (5,2) (1,4) (8,9) (7,10) (2,9) (3,10) (3,2) (5,2) (1,4)
81 82
IMPLEMENTATION IMPLEMENTATION
83 84
IMPLEMENTATION IMPLEMENTATION
} }
85 86
IMPLEMENTATION IMPLEMENTATION
IMPLEMENTATION
THANK YOU !
Node* startState = makeNode(sr,sc,0); int nr = s->row + dr[k];int nc= s->col + dc[k];
push(startState); visited[sr][sc] = 1; if(visited[nr][nc]==0&& A[nr][nc]==0){
} Node* ns = makeNode(nr, nc, s->step + 1);
push(ns);visited[nr][nc] = 1;
int main(){ if(targetState(ns)) return ns->step;
input(); }
int res = solve(); }
printf("%d",res); }
return 0; return -1;
} }
91 92