Computational Logic and Computational Thinking Programming ...
Transcript of Computational Logic and Computational Thinking Programming ...
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
1
Computational Logic and Programming Applications
Computational Thinking & Algorithms (II)
Department of Accounting InformationChihlee University of TechnologyChien-Hua Tsai
http://www1.chihlee.edu.tw/teachers/chienhua/
Chien-Hua Tsai 2
Roadmap
> Scheduling> Backtracking> Planning and Optimization> Pipelining
Computational Thinking
3
Roadmap
> Scheduling> Backtracking> Planning and Optimization> Pipelining
Computational Thinking
Chien-Hua Tsai
Computational Thinking
4Chien-Hua Tsai
> Algorithms are at the heart of Computational Thinking and Computer Science, because in Computer Science the solutions to problems are not simply an answer (e.g. ‘75’, or a fact), they are algorithms.
Computational Thinking & Algorithms
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
2
AlgorithmsComputational Thinking
5
An algorithm is a sequence of instructions, or set of rules, for performing a task
An algorithm is a sequence of instructions, or set of rules, for performing a task
Chien-Hua Tsai
> A step by step procedure to solve a problem or explain a process.
> A plan of the steps you would have to go through to solve a problem.
Process ManagementComputational Thinking
6Chien-Hua Tsai
> As a process executes, it changes state:— new: The process is being created— ready: The process is waiting to be assigned to run— running: Instructions are being executed— waiting: The process waiting for some event to occur— terminated: The process has finished execution
Multiprocessing vs. MultiprogrammingComputational Thinking
7Chien-Hua Tsai
> Multi-processing: Supports running a program on more than one CPU.
> Multi-programming (or, multi-tasking): Allows more than one program to run concurrently.
> Multi-threading: Allows different parts of a single program to run concurrently.
> Remember Definitions:— Multi-processing Multiple CPUs— Multi-programming Multiple programs— Multi-threading Multiple threads per program
> What does it mean to run two threads “concurrently”?— Threads run in any order and interleaving
Computational Thinking
8Chien-Hua Tsai
Multiprocessing vs. Multiprogramming
A B C
BA ACB C BMultiprogramming
ABC
Multiprocessing
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
3
Can threads make this easier?Computational Thinking
9Chien-Hua Tsai
> Most of the time, threads are working on separate data, so scheduling doesn’t matter:
Thread A Thread Bx = 1; y = 2;
> However, what about (initially, y = 12):Thread A Thread B
x = 1; y = 2;x = y+1; y = y2;
— What are the possible values of x?
Scheduling AssumptionsComputational Thinking
10Chien-Hua Tsai
> CPU scheduling big area of research in early 70s> Many implicit assumptions for CPU scheduling:
— One program per user— One thread per program— Programs are independent
> The high-level goal: dole out CPU time to optimize some desired parameters of system
> Maximum CPU utilization obtained with multiprogramming
> CPU utilization – keep the CPU as busy as possible> Throughput – # of processes that complete their
execution per time unit
First-Come, First-Served (FCFS) SchedulingComputational Thinking
11Chien-Hua Tsai
Process Burst TimeP1 24P2 3P3 3
> Suppose that the processes arrive in the order: P1, P2, P3The Gantt Chart for the schedule is:
> Waiting time for P1 = 0; P2 = 24; P3 = 27> Average waiting time: (0 + 24 + 27)/3 = 17> Average completion time: (24 + 27 + 30)/3 = 27
P1 P2 P3
24 27 300
First-Come, First-Served (FCFS) SchedulingComputational Thinking
12Chien-Hua Tsai
> Suppose that the processes arrive in the orderP2, P3, P1
> The Gantt chart for the schedule is:
> Waiting time for P1 = 6; P2 = 0; P3 = 3> Average waiting time: (6 + 0 + 3)/3 = 3> Average completion time: (3 + 6 + 30)/3 = 13> Much better than previous case
P1P3P2
63 300
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
4
Computational Thinking
13Chien-Hua Tsai
Your Turn: FCFS Scheduling
Job Arrival CPU Burst
A 0 10B 1 2C 2 4
What is the Gantt chart for the schedule?What is the average waiting time?
Shortest-Job-First (SJF) SchedulingComputational Thinking
14Chien-Hua Tsai
Process Arrival Time Burst TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4
> The Gantt chart for the schedule is:
> Average waiting time = (0 + 6 + 3 + 7)/4 = 4> Average completion time = (7 + 10 + 4 + 11)/4 = 8
P1 P3 P2
73 160
P4
8 12
Shortest-Remaining-Time-First (SRTF) Scheduling
Computational Thinking
15Chien-Hua Tsai
Process Arrival Time Burst TimeP1 0.0 7P2 2.0 4P3 4.0 1P4 5.0 4
> The Gantt chart for the schedule is:
> Average waiting time = (9 + 1 + 0 + 2)/4 = 3> Average completion time = (16 + 5 + 1 + 6)/4 = 7
P1 P3P2
42 110
P4
5 7
P2 P1
16
Computational Thinking
16Chien-Hua Tsai
Your Turn: SJF & SRTF Scheduling
Job Arrival CPU BurstA 0 8B 1 4C 2 9D 3 5
What are the Gantt charts for SJF and SRTF?What is the average waiting time respectively?
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
5
Priority SchedulingComputational Thinking
17Chien-Hua Tsai
Process Burst Time PriorityP1 10 3P2 1 1P3 2 4P4 1 5P5 5 2
> The Gantt chart for the schedule is:
> Average waiting time: (6 + 0 + 16 + 18 + 1)/5 = 8.2> Average completion time: (16 + 1 + 18 + 19 + 6)/5 = 12
P1P5P2
61 190
P3 P4
16 18
Computational Thinking
18Chien-Hua Tsai
Your Turn: Priority Scheduling
Job Arrival CPU Burst PriorityA 0 4 4B 1 3 3C 2 1 2D 3 5 1E 4 2 1
What are the Gantt charts for he schedule?What is the average waiting time?
Round Robin (RR) Scheduling with Time Quantum = 20
Computational Thinking
19Chien-Hua Tsai
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
Process Burst TimeP1 53P2 17P3 68P4 24
> The Gantt chart for the schedule is:
> Waiting time for P1 = (77–20)+(121–97) = 81P2 = (20–0) = 20 P3 = (37–0)+(97–57)+(134–117) = 94P4 = (57–0)+(117–77) = 97
Round Robin (RR) Scheduling with Time Quantum = 20
Computational Thinking
20Chien-Hua Tsai
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
Process Burst TimeP1 53P2 17P3 68P4 24
> The Gantt chart for the schedule is:
> Average waiting time = (81+20+94+97)/4=73> Average completion time = (134+37+162+121)/4=113.5
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
6
Computational Thinking
21Chien-Hua Tsai
Your Turn: RR Scheduling with Time Quantum = 1
Job Arrival CPU Burst
A 0 10B 1 2C 2 4
What is the Gantt chart for the schedule?What is the average waiting time?
22
Roadmap
> Scheduling> Backtracking> Planning and Optimization> Pipelining
Computational Thinking
Chien-Hua Tsai
BacktrackingComputational Thinking
23Chien-Hua Tsai
> Backtracking is a methodical way of trying out various sequences of decisions, until you find one that “works”
> At each intersection, you have to decide between three or fewer choices:— Go straight— Go left— Go right
Computational Thinking
24Chien-Hua Tsai
Backtracking Problem: An Example
> You wish to find a goal state
CC DD
AA BB
EE FF
RootRoot
badbad badbad badbadgoodgood
1. Starting at Root, your options are A and B. You choose A.
2. At A, your options are C and D. You choose C.
3. C is bad. Go back to A.
4. At A, you have already tried C, and it failed. Try D.
5. D is bad. Go back to A.
6. At A, you have no options left to try. Go back to Root.
7. At Root, you have already tried A. Try B.
8. At B, your options are E and F. Try E.
9. E is good. Congratulations!
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
7
Computational Thinking
25Chien-Hua Tsai
Your Turn: Backtracking for Finding Permutations of a given string
> Given a set, enumerate all possible permutations.— Input: {1, 2, 3}— Output: { [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1] }— Permutation describes an arrangement or ordering of items.
Computational Thinking
26Chien-Hua Tsai
Your Turn: Backtracking for Finding Combinations of a given string
> Given a set, print all its combinations.— Input: {1, 2, 3}— Output: { {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} }— It involves a set and none of the subsets repeats itself, but just
changing the order.
27
Roadmap
> Scheduling> Backtracking> Planning and Optimization> Pipelining
Computational Thinking
Chien-Hua Tsai
Planning and OptimizationComputational Thinking
28Chien-Hua Tsai
> Planning is a search problem that requires to find an efficient sequence of actions that transform a system from a given starting state to the goal state— Planning may require
sophisticated analysis of multiple scenario
> For any given optimization problem, we would like to come up with a (hopefully efficient) algorithm— That ideally finds the global
minimum of the objective function
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
8
Computational Thinking
29Chien-Hua Tsai
Planning & Optimization: An Example
> The shortest path problem is the problem of finding a path between two nodes such that the sum of the weights of its constituent edges is minimized.
Computational Thinking
30Chien-Hua Tsai
Planning & Optimization: An Example
> Kruskal's Algorithm
Computational Thinking
31Chien-Hua Tsai
Planning & Optimization: An Example
> Kruskal's Algorithm
Computational Thinking
32Chien-Hua Tsai
Planning & Optimization: An Example
> Prim's Algorithm
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
9
Computational Thinking
33Chien-Hua Tsai
Planning & Optimization: An Example
> Prim's Algorithm
Computational Thinking
34Chien-Hua Tsai
Your Turn: Planning and Optimization
> Given a graph and a source vertex in the graph, find shortest paths from source to all vertices by Kruskal's and Prim's algorithms respectively.
Computational Thinking
35Chien-Hua Tsai
Open Shortest-Path First (OSPF) Routing(Dijkstra's Algorithm)
Network is a graph with routers and linksEach unidirectional link has a weightShortest-path routes from sum of link weights
Weights are assigned statically (configuration file)Weights based on capacity, distance, and trafficFlooding of info about weights and IP addresses
u
yx
wv
z2
21
3
1
12
53
5
Graph: G = (N,E)N = set of routers = { u, v, w, x, y, z }E = set of links ={ (u,v), (u,x), (v,x), (v,w), (x,w), (x,y), (w,y), (w,z), (y,z) }
link
router
Computational Thinking
36Chien-Hua Tsai
Building OSPF Routing Table
8
1712
10 20
32
12
14 A
B
E
F D
C
A(0)
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
10
Computational Thinking
37Chien-Hua Tsai
Building OSPF Routing Table
8
1712
10 20
32
12
14 A
B
E
F D
C
A(0)
B(8)
D(20)
C(32)
Computational Thinking
38Chien-Hua Tsai
Building OSPF Routing Table
8
1712
10 20
32
12
14 A
B
E
F D
C
A(0)
B(8)
D(20)
C(32)
D(25)
Computational Thinking
39Chien-Hua Tsai
Building OSPF Routing Table
8
1712
10 20
32
12
14 A
B
E
F D
C
A(0)
B(8)
D(20)
C(32)
D(25)
F(32)
C(30)
Computational Thinking
40Chien-Hua Tsai
Building OSPF Routing Table
A(0)
B(8)
D(20)
C(32)
D(25)
F(32)
C(30)
8
1712
10 20
32
12
14 A
B
E
F D
C
F(42)
E(44)
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
11
Computational Thinking
41Chien-Hua Tsai
Building OSPF Routing Table
A(0)
B(8)
D(20)
C(32)
D(25)
F(32)
C(30)
8
1712
10 20
32
12
14 A
B
E
F D
C
F(42)
E(44)
Computational Thinking
42Chien-Hua Tsai
Building OSPF Routing Table
A(0)
B(8)
D(20)
C(32)
D(25)
F(32)
C(30)
8
1712
10 20
32
12
14 A
B
E
F D
C
F(42)
E(44)
Computational Thinking
43Chien-Hua Tsai
Building OSPF Routing Table
8
1712
10 20
32
12
14 A
B
E
F D
C
A(0)
B(8)
D(20)
F(32)
C(30)
E(44)
Computational Thinking
44Chien-Hua Tsai
Building OSPF Routing Table
Forwarding table
The shortest path treeof router A
A(0)
B(8)
D(20)
F(32)
C(30)
E(44)
Destination Next hop
Shortest path
Total cost
B B A B 8
C D A D C 30
D D A D 20
E D A D C E 44
F D A D F 32
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
12
Computational Thinking
45Chien-Hua Tsai
Your Turn: Building Routing Table
8
1712
10
32
12
14 A
B
E
F D
C
Check the case!
Computational Thinking
46Chien-Hua Tsai
Your Turn: Building Routing Table
Forwarding table
The shortest path treeof router A
Destination Next hop
Shortest path
Total cost
B
C
D
E
F
47
Roadmap
> Scheduling> Backtracking> Planning and Optimization> Pipelining
Computational Thinking
Chien-Hua Tsai
PipeliningComputational Thinking
48Chien-Hua Tsai
> Pipelining is an implementation technique where multiple instructions are overlapped in execution.
> The pipeline designer's goal is to balance the length of each pipeline stage.
Chihlee University of Technology, Fall 2020Computational Logic & Programming ApplicationsAssociate Professor: Chien-Hua Tsai
2020/10/23
13
Computational Thinking
49Chien-Hua Tsai
Pipelining: An Example
> Ann, Brian, Cathy, Dave each have one load of clothes to wash, dry, and fold— Washer takes 30 minutes— Dryer takes 40 minutes— Folder takes 20 minutes
Computational Thinking
50Chien-Hua Tsai
Your Turn: Pipelining Arrangement
> Consider a processor having 4 stages and let there be 2 instructions to be executed.
> We can visualize the execution sequence in the space-time diagram at the right.
What does it need a total of cycles for execution in a pipelined processor?
What does it need a total of cycles for execution in a pipelined processor?
Reading AssignmentComputational Thinking
51
> Related materials are covered in the lecture slides
Chien-Hua Tsai
Computational Logic and Programming Applications
Computational Thinking & Algorithms (II)
End of the Lecture