one problem lets you know thoroughly the difference between the Dijkstra and Distance-Vector

1 Problem Description:

在这里插入图片描述

2 The realization of Code for the 2 algorithms

the following Link is the realization of dijkstra algorithm.

below is the realization of distance-vector algorithm.

3 Solutions for 5 subproblems

1 the shortest path from v1 to v7 is v1 -> v3 -> v7 with the minimal distance of 4.

2 no matter what algorithm we adopt, after convergence, the forwarding table of each node is as follow:
(1) the forwarding table of the v1 node:
destination nodedistancenext stepv25v2v32v3v43v3v56v3/v2v65v3v74v3

(2) the forwarding table of the v2 node:
destination nodedistancenext stepv15v2v37v1v48v1v51v5v68v5v77v5

(3) the forwarding table of the v3 node:
destination nodedistancenext stepv12v1v27v1v41v4v54v5v63v6v72v7

(4) the forwarding table of the v4 node:
destination nodedistancenext stepv13v3v28v3v31v4v55v3v62v6v73v3

(5) the forwarding table of the v5 node:
destination nodedistancenext stepv16v3/v2v21v2v34v3v45v3v67v3v76v7

(6) the forwarding table of the v6 node:
destination nodedistancenext stepv16v3/v2v21v2v34v3v45v3v67v3v76v7

(7) the forwarding table of the v7 node:
destination nodedistancenext stepv14v3v27v5v32v3v44v3v56v5v65v3

3
3.1
There seems to be 2 solutions for this subpart. However, the real answer is 2. You can give a glance at these 2 proving process for better understanding the d-v alg;
proof:(Real Answer)
(1) when adopting the Dikjstra, a mere one iteration will contribute to the convergence of all nodes’ forwarding tables. This is caused by that this kind of Link state algorithm allows all nodes to send nearby nodes’ state to all other nodes, which determines the number of iterations after the c(v3, v6) becomes 50.
(2) If using the d-v algorithm, there would be 2.

  1. To begin with, once the distance shifts from 3 to 50, the v3 node would feel and recalculate the minimal distance in the routing table. At this point, the v3 node will query other nodes’ state by local cache, and compare whether it is necessary to update
    the shortest distance from itself to v6. Since the local cache has store previous data from v4, v3 node will conclude that it is 1 unit distance away from v4, v4 2 units of distance away from v6, and the toal distance from itself to v4 and then v6 is the
    shortest path. That is 3. The only change of fowarding table in node v3 is the next hop from v3 to v6.
    v6 node would experience the same process of computing and propagating.
  2. Because v3’s forwarding table changes, node 3 will send the update info to nearby nodes. Though knowing the change, v1 still finds the shortest distance from v3 to v6 is 3. Therefore it will not modify its forwarding table item. So does other nearby nodes of v3.

3.2
proof:
(1) when adopting the Dikjstra, a mere one iteration will contribute to the convergence of all nodes’ forwarding tables. This is caused by that this kind of Link state algorithm allows all nodes to send nearby nodes’ state to all other nodes, which determines the number of iterations after the c(v3, v6) becomes 50.
(2) If using the d-v algorithm, there would be 4.

  1. To begin with, once the distance shifts from 3 to 50, the v3 node would feel and recalculate the minimal distance in the routing table. Because the v3 node is connected with all other nodes of the network. Then, v3 will send its state to all nodes but itself.
  2. after sensing the changed item from v3, v1 node would update its distance to v6 to 52, V7 will update the distance to v6 from 5 to 52, and V5’s distance to v6 will be changed to 54. And then these nodes whose forwarding table items have changed would propagate the change to their neighborhood.
  3. After v2 node receives the link state from v5 with distance to v6 54 and v1 with distance to v6 now 52 , the v2’s shortest distance to v6 becomes min{distance(v1, v6) + 5, distance(v5, v6) + 1} = 55.
  4. Eventually, the v2 node will spread its changed minimal distance to its near nodes. These nodes find nothing changed and the whole network converges.

4
(1) when adopting the Dikjstra, a mere one iteration will contribute to the convergence of all nodes’ forwarding tables. The cause is as mentioned above in 3(1) part.
(2) If using the d-v algorithm, the iterations will be 4.

  1. Initailly, the v3 node will update the shortest path, and send its change of routing info to its nearby nodes
  2. Next, after receiving the info from v3, the v5 node finds that the distance from v3 to v6 has changed to 1. So, the v5 node updates the shortest distance from itself to v6 to 5. Referring to the v5 node, the node v7 would shift the shortest distance to v6 to 3.
    and v1 node will update the distance to 3.
  3. Subsequently, v5, v1, and v7 node will transmit individual state to their nearby nodes. After absorbing data from v5, the v2 node detects that the shortest distance to v6 should be altered to 6.
  4. Finally, the v2 tells v1, v3, and v5 node the changed routing table. And these nodes discover there is no updated item after comparing. At this time, the overall network converges.
    Therefore, it will spend 4 times on converging.

5
(1)when it comes to the distance vector algorithm, it will get into the dead loop. That’s to say, the iterations will bounce between v3 and v5 back and forth.

  1. Considering the diagram below, after detecting the link cost shifts to -10, v3 would know its shortest distance to v2 is via v5 and -9 units of distances. v5 also knows that its shortest distance to v2 is -2. Then at this iteration, v3 and v5 will send induvidual state to other nodes.
  2. After receiving data from v3, v2 learns that its shortest distance to v3 will be -9 via v5 and to v5 will be -2 via v3. Then the v2 node would propagate the changed item to v3 and v5.
    3)Based on the new info from v2, v3 will update its shortest distance to v5 to -11 via v5(because v2’s shortest distance to v5 -2 will be gotten by v3). After computing, the v3 node will send data to its nearby v5 and v2. So does v5 node.
  3. because every change in the forwarding table of each node will contribute to the propogation and the change of v3 and v5 all the time occurs, the infinite loop forms at this time.

v2 now knows that going through the v3 to v5 is at the least cost. So v2 node would update its forwarding table and send changed data to v5 and v3.
2) v3 calculates that it is only

(2)
Dijkstra does not allow for the negative value of its edges. This is because if the weight value of the edge becomes negative, some point named a in the set S(that has been definite and will not change once points are joint into it) may be connected with bby the negative edge. That’s to say, the shortest path that originally determined between A and B might be bigger than new calculated one. At this point, we should update the shortest path but we cannot do it due to the rule that cannot modify the shortest path of points having become part of set S.
Referring to the figure below, suppose the node v2 has been added to the shortest path set S, then we added the node v3 to S and determine the shortest path from v2 to v3 is 8. Then we added node v5 to the set S. At this point, we find that the distance between v5 and v3 is -10, which indicates that the minimal distance between v2 and v3 is min(A->B, A->C->B) = -1. However, we cannot update the shortest distance between A and B. So this is the troublesome.

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's a possible implementation of the game using the classes described: ```python import random class Die: """Class representing a 6-sided die.""" def __init__(self): self.value = None def roll(self): """Rolls the die and sets its value to a random integer between 1 and 6.""" self.value = random.randint(1, 6) class DiceCup: """Class representing a cup containing five dice.""" def __init__(self): self.dice = [Die() for _ in range(5)] self.banked_dice = [] def roll(self): """Rolls all non-banked dice in the cup.""" for die in self.dice: if die not in self.banked_dice: die.roll() def bank(self, index): """Banks the die at the specified index.""" self.banked_dice.append(self.dice[index]) del self.dice[index] def release(self, index): """Releases the die at the specified index from the bank.""" self.dice.append(self.banked_dice[index]) del self.banked_dice[index] class ShipOfFoolsGame: """Class representing the game logic.""" def __init__(self, winning_score): self.winning_score = winning_score def play_round(self, players): """Plays one round of the game with the given players.""" for player in players: player.play_round(self) winner = max(players, key=lambda p: p.score) print(f"Player {winner.id} wins the round with a score of {winner.score}") if winner.score >= self.winning_score: print(f"Player {winner.id} wins the game with a total score of {winner.score}") return True else: return False class Player: """Class representing a player.""" def __init__(self, id): self.id = id self.score = 0 def play_round(self, game): """Plays one round of the game.""" cup = DiceCup() cup.roll() for i in range(5): if i in [0, 1, 2] and cup.dice[i].value == i+4: cup.bank(i) elif i == 3 and cup.dice[i].value == 5: cup.bank(i) elif i == 4 and cup.dice[i].value == 6: cup.bank(i) cup.roll() self.score += sum(cup.banked_dice) class PlayRoom: """Class representing a room with multiple players and a game.""" def __init__(self, num_players, winning_score): self.players = [Player(i+1) for i in range(num_players)] self.game = ShipOfFoolsGame(winning_score) def play_game(self): """Plays the game until a player reaches the winning score.""" while not self.game.play_round(self.players): pass ``` In this implementation, the `Die` class represents a 6-sided die and has a `roll()` method that generates a random value between 1 and 6. The `DiceCup` class represents a cup containing five dice and has methods for rolling all non-banked dice, banking and releasing individual dice, and calculating the score of the banked dice. The `ShipOfFoolsGame` class represents the game logic and has a `play_round()` method that lets each player play a round of the game, calculates the winner, and checks if the game has been won. The `Player` class represents a player and has a `play_round()` method that plays a round of the game for that player, using a `DiceCup` object and following the game rules. Finally, the `PlayRoom` class represents a room with multiple players and a `ShipOfFoolsGame` object, and has a `play_game()` method that plays the game until a player reaches the winning score.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值