159.201 Algorithms & Data Structures Tutorial 3
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
159.201 Algorithms & Data Structures
Tutorial 3
Answers
1. For assignment 3 you need to implement a method to check the number of elements of a queue implemented with a LL. Write a pseudocode for a method or function called Length() that can do that. Discuss aspects such as: return value, extra pointers or variables for better performance.
Answer:
The method Length() could rely on a counter being updated by Join() and Leave():
int Queue::Length() {
return counter;
}
Or, if you do not want to add the variable, we have to traverse the linkedlist:
int Queue::Length() {
Node *temp=front;
int counter=0;
while(temp!=NULL){
temp=temp>next;
counter++;
}
return counter;
}
Traversing long linkedlists have performance consequences, and having a counter updated by Leave( ) and Join( ) would be a better approach.
2. For assignment 3, use the following data to simulate the router, and to get the appropriate outputs for each queue:
## Router simulation
Ports 3
## each line represents the queue for a single input port
## each number represents the destination of the packet
## this router has N=3 ports
2 2 2 2 3 3
1 1 3 3 3 3
1 2 2 1 2 1
Answer:
There are 6 queues, 3 input queues and 3 output queues. Lets call them InpQ[i] and OutQ[i]. We also need an array to keep the maximum congestion size at any point.
We start by loading the input queues with the data (considering that the head of the queue is on the left, and the tail is on the right), and with empty output queues:
InpQ[0] = { 2 2 2 2 3 3 } InpQ[1] = { 1 1 3 3 3 3 } InpQ[2] = { 1 2 2 1 2 1 }
OutQ[0] = { }
OutQ[1] = { }
OutQ[2] = { }
Congestion_Size[0] = 0
Congestion_Size[1] = 0
Congestion_Size[2] = 0
The simulation starts by moving one packet at a time, and at
time = clock % (TIMEDELAY*number_of_ports) == 0,
with TIMEDELAY=3 and number_of_ports=3. Therefore queues can have one packet removed when t=9, t=18, t=27 etc...
Time t=0: port 1 transfers a packet 2 to port 2 |
|
|
|
InpQ[0] = { 2 2 2 3 3 } OutQ[0] = { } |
Congestion_Size[0] = 0 |
|
|
InpQ[1] = { 1 1 3 3 3 3 } OutQ[1] = { 2 } |
Congestion_Size[1] = 1 |
sum=1 |
max_sum=1 |
InpQ[2] = { 1 2 2 1 2 1 } OutQ[2] = { } |
Congestion_Size[2] = 0 |
|
|
Time t=1: port 2 transfers a packet 1 to port 1 |
|
|
|
InpQ[0] = { 2 2 2 3 3 } OutQ[0] = { 1 } |
Congestion_Size[0] = 1 |
|
|
InpQ[1] = { 1 3 3 3 3 } OutQ[1] = { 2 } |
Congestion_Size[1] = 1 |
sum=2 |
max_sum=2 |
InpQ[2] = { 1 2 2 1 2 1 } OutQ[2] = { } |
Congestion_Size[2] = 0 |
|
|
Time t=2: port 3 transfers a packet 1 to port 1 |
|
|
|
InpQ[0] = { 2 2 2 3 3 } OutQ[0] = { 1 1 } |
Congestion_Size[0] = 2 |
|
|
InpQ[1] = { 1 3 3 3 3 } OutQ[1] = { 2 } |
Congestion_Size[1] = 1 |
sum=3 |
max_sum=3 |
InpQ[2] = { 2 2 1 2 1 } OutQ[2] = { } |
Congestion_Size[2] = 0 |
|
|
Time t=3: port 1 transfers a packet 2 to port 2 |
|
|
|
InpQ[0] = { 2 2 3 3 } OutQ[0] = { 1 1 } |
Congestion_Size[0] = 2 |
|
|
InpQ[1] = { 1 3 3 3 3 } OutQ[1] = { 2 2 } InpQ[2] = { 2 2 1 2 1 } OutQ[2] = { } |
Congestion_Size[1] = 2 Congestion_Size[2] = 0 |
sum=4 |
max_sum=4 |
Time t=4: port 2 transfers a packet 1 to port 1
InpQ[0] = { 2 2 3 3 } OutQ[0] = { 1 1 1 }
InpQ[1] = { 3 3 3 3 } OutQ[1] = { 2 2 }
InpQ[2] = { 2 2 1 2 1 } OutQ[2] = { }
Time t=5: port 3 transfers a packet 2 to port 2
InpQ[0] = { 2 2 3 3 } InpQ[1] = { 3 3 3 3 } InpQ[2] = { 2 1 2 1 } |
OutQ[0] = { 1 1 1 } OutQ[1] = { 2 2 2 } OutQ[2] = { } |
Time t=6: port 1 transfers a packet 2 to port 2
InpQ[0] = { 2 3 3 } InpQ[1] = { 3 3 3 3 } InpQ[2] = { 2 1 2 1 } |
OutQ[0] = { 1 1 1 } OutQ[1] = { 2 2 2 2 } OutQ[2] = { } |
Time t=7: port 2 transfers a packet 3 to port 3
InpQ[0] = { 2 3 3 } InpQ[1] = { 3 3 3 } InpQ[2] = { 2 1 2 1 } |
OutQ[0] = { 1 1 1 } OutQ[1] = { 2 2 2 2 } OutQ[2] = { 3 } |
Time t=8: port 3 transfers a packet 2 to port 2
OutQ[0] = { 1 1 1 } OutQ[1] = { 2 2 2 2 2 } OutQ[2] = { 3 }
Time t=9: port 1 transfers a packet 2 to port 2
OutQ[0] = { 1 1 1 } OutQ[1] = { 2 2 2 2 2 2 } OutQ[2] = { 3 }
One packet leaves each queue, then we verify the sum
OutQ[0] = { 1 1 } OutQ[1] = { 2 2 2 2 2 } OutQ[2] = { }
Congestion_Size[0] = 3
Congestion_Size[1] = 2
Congestion_Size[2] = 0
Congestion_Size[0] = 3
Congestion_Size[1] = 3
Congestion_Size[2] = 0
Congestion_Size[0] = 3
Congestion_Size[1] = 4
Congestion_Size[2] = 0
Congestion_Size[0] = 3
Congestion_Size[1] = 4
Congestion_Size[2] = 1
Congestion_Size[0] = 3
Congestion_Size[1] = 5
Congestion_Size[2] = 1
Congestion_Size[0] = 3
Congestion_Size[1] = 6
Congestion_Size[2] = 1
Congestion_Size[0] = 2
Congestion_Size[1] = 5
Congestion_Size[2] = 0
Therefore, the number of packets in the most congestion situation so far (with max_sum=9) is
queue 1: 3 pkts, in queue 2: 5 pkts, and in queue 3: 1 pkts.
Time t=10: port 2 |
transfers a packet 3 to port 3 |
|
|
InpQ[0] = { 3 3 } |
OutQ[0] = { 1 1 } |
Congestion_Size[0] = 2 |
|
InpQ[1] = { 3 3 } |
OutQ[1] = { 2 2 2 2 2 } |
Congestion_Size[1] = 5 |
sum=8 max_sum=9 |
InpQ[2] = { 1 2 1 |
} OutQ[2] = { 3 } |
Congestion_Size[2] = 1 |
|
Time t=11: port 3 |
transfers a packet 1 to port 1 |
|
|
InpQ[0] = { 3 3 } |
OutQ[0] = { 1 1 1} |
Congestion_Size[0] = 3 |
|
InpQ[1] = { 3 3 } |
OutQ[1] = { 2 2 2 2 2 } |
Congestion_Size[1] = 5 |
sum=9 max_sum=9 |
InpQ[2] = { 2 1 } |
OutQ[2] = { 3 } |
Congestion_Size[2] = 1 |
|
Time t=12: port 1 |
transfers a packet 3 to port 3 |
|
|
InpQ[0] = { 3 } |
OutQ[0] = { 1 1 1} |
Congestion_Size[0] = 3 |
|
InpQ[1] = { 3 3 } |
OutQ[1] = { 2 2 2 2 2 } |
Congestion_Size[1] = 5 |
sum=10 max_sum=10 |
InpQ[2] = { 2 1 } |
OutQ[2] = { 3 3} |
Congestion_Size[2] = 2 |
|
Time t=13: port 2 |
transfers a packet 3 to port 3 |
|
|
InpQ[0] = { 3 } |
OutQ[0] = { 1 1 1} |
Congestion_Size[0] = 3 |
|
InpQ[1] = { 3 } |
OutQ[1] = { 2 2 2 2 2 } |
Congestion_Size[1] = 5 |
sum=11 max_sum |
2023-04-13