Escolar Documentos
Profissional Documentos
Cultura Documentos
Spanning Tree
Spanning tree is a subset of graph/net. The tree is base of a root node. Usage: others algorithms that works on the spanning tree, without warring loops.
Problem description
Algorithms that build a spanning tree and select a leader. No know root The root of the spanning tree will be the leader Each node has unique ID
Solution
The Leader is the node with the higher ID value. Each node in the system selects it self as a leader, and starts DFS algorithm.
Algorithm
1: 2: 3: 4: 5:
6: upon receiving <LEADER, new-id> from Pj 7: if leader < new-id then 8: leader := new-id 9: parent := Pj 10: CH:= {} 11: UN:= all neighbors of Pi except Pj 12: explore() 13: else if leader = new-id then 14: send <ALREADY, leader> to Pj 15: upon receiving <ALREADY, new-id> from Pj 16: if new-id = leader then explore() 17: upon receiving <PARENT, new-id> from Pj 18: if new-id = leader then 19: add Pj to CH 20: explore()
21: procedure explore() 22: if UN <> {} then 23: let Pk be a processor in UN 24: remove Pk from UN 25: send <LEADER, leader> to Pk 26: else 27: if parent <> Pi then send <PARENT, leader> to parent 28: else terminate as root of spanning tree
1: 2: 3: 4: 5:
6: upon receiving <LEADER, new-id> 7: if leader < new-id then 8: leader := new-id 9: parent := Pj 10: CH:= {} 11: UN:= all neighbors of Pi 12: explore() 13: else if leader = new-id then 14: send <ALREADY, leader> to Pj
Each node has 5 data members 1) ID from Pj2) Leader 3) Parent 4) CH children list except 5) UN List of neighbors Pj
15: upon receiving <ALREADY, new-id> from Pj 16: if new-id = leader then explore() 17: upon receiving <PARENT, new-id> from Pj 18: if new-id = leader then 19: add Pj to CH 20: explore()
21: procedure explore() 22: if UN <> {} then 23: let Pk be a processor in UN 24: remove Pk from UN 25: send <LEADER, leader> to Pk 26: else 27: if parent <> Pi then send <PARENT, leader> to parent 28: else terminate as root of spanning tree
1: 2: 3: 4: 5:
Init procedure
6: upon receiving <LEADER, new-id> from Pj 7: if leader < new-id then 8: leader := new-id 9: parent := Pj 10: CH:= {} 11: UN:= all neighbors of Pi except Pj 12: explore() 13: else if leader = new-id then 14: send <ALREADY, leader> to Pj 15: upon receiving <ALREADY, new-id> from Pj 16: if new-id = leader then explore() 17: upon receiving <PARENT, new-id> from Pj 18: if new-id = leader then 19: add Pj to CH 20: explore()
21: procedure explore() 22: if UN <> {} then 23: let Pk be a processor in UN 24: remove Pk from UN 25: send <LEADER, leader> to Pk 26: else 27: if parent <> Pi then send <PARENT, leader> to parent 28: else terminate as root of spanning tree
1: 2: 3: 4: 5:
6: upon receiving <LEADER, new-id> from Pj 7: if leader < new-id then 8: leader := new-id 9: parent := Pj 10: CH:= {} 11: UN:= all neighbors of Pi except Pj 12: explore() 13: else if leader = new-id then 14: send <ALREADY, leader> to Pj 15: upon receiving <ALREADY, new-id> from Pj 16: if new-id = leader then explore() 17: upon receiving <PARENT, new-id> from Pj 18: if new-id = leader then 19: add Pj to CH 20: explore()
21: procedure explore() 22: if UN <> {} then 23: let Pk be a processor in UN 24: remove Pk from UN 25: send <LEADER, leader> to Pk 26: else 27: if parent <> Pi then send <PARENT, leader> to parent 28: else terminate as root of spanning tree
explore procedure
1: 2: 3: 4: 5:
6: upon receiving <LEADER, new-id> from Pj 7: if leader < new-id then 8: leader := new-id 9: parent := Pj 10: CH:= {} 11: UN:= all neighbors of Pi except Pj 12: explore() 13: else if leader = new-id then 14: send <ALREADY, leader> to Pj 15: upon receiving <ALREADY, new-id> from Pj 16: if new-id = leader then explore() 17: upon receiving <PARENT, new-id> from Pj 18: if new-id = leader then 19: add Pj to CH 20: explore()
21: procedure explore() 22: if UN <> {} then 23: let Pk be a processor in UN 24: remove Pk from UN 25: send <LEADER, leader> to Pk 26: else 27: if parent <> Pi then send <PARENT, leader> to parent 28: else terminate as root of spanning tree
1: 2: 3: 4: 5:
6: upon receiving <LEADER, new-id> from Pj 7: if leader < new-id then 8: leader := new-id 9: parent := Pj 10: CH:= {} 11: UN:= all neighbors of Pi except Pj 12: explore() 13: else if leader = new-id then 14: send <ALREADY, leader> to Pj 15: upon receiving <ALREADY, new-id> from Pj 16: if new-id = leader then explore() 17: upon receiving <PARENT, new-id> from Pj 18: if new-id = leader then 19: add Pj to CH 20: explore()
21: procedure explore() 22: if UN <> {} then 23: let Pk be a processor in UN 24: remove Pk from UN 25: send <LEADER, leader> to Pk 26: else 27: if parent <> Pi then send <PARENT, leader> to parent 28: else terminate as root of spanning tree
Example
P: L: UN: CH: P: L: UN: CH:
P: L: UN: CH:
P: L: UN: CH:
STEP: 0
Example
P: 1 L: 1 UN: {2,4} CH: {} P: 2 L: 2 UN: {1,3,4} CH: {}
STEP: 1
Example
P: 1 L: 1 UN: {2} CH: {} P: 2 L: 2 UN: {1,3} CH: {}
P: 3 L: 3 UN: {} CH: {}
STEP: 2
Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {1,3} CH: {}
P: 3 L: 3 UN: {} CH: {}
STEP: 3
Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {1,3} CH: {}
P: 3 L: 3 UN: {} CH: {}
STEP: 4
Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {1} CH: {}
P: 3 L: 3 UN: {} CH: {}
STEP: 5
Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {1} CH: {}
P: 2 L: 4 UN: {} CH: {}
STEP: 6
Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {1} CH: {}
P: 2 L: 4 UN: {} CH: {}
STEP: 7
Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {1} CH: {3}
P: 2 L: 4 UN: {} CH: {}
STEP: 8
Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {} CH: {3}
P: 2 L: 4 UN: {} CH: {}
STEP: 9
Example
P: 2 L: 4 UN: {4} CH: {} P: 4 L: 4 UN: {} CH: {3}
P: 2 L: 4 UN: {} CH: {}
STEP: 10
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {3}
P: 2 L: 4 UN: {} CH: {}
STEP: 11
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {3}
P: 2 L: 4 UN: {} CH: {}
STEP: 12
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {3}
P: 2 L: 4 UN: {} CH: {}
STEP: 13
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}
P: 2 L: 4 UN: {} CH: {}
STEP: 14
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}
P: 2 L: 4 UN: {} CH: {}
STEP: 15
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}
P: 2 L: 4 UN: {} CH: {}
STEP: 16
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}
P: 2 L: 4 UN: {} CH: {}
STEP: 17
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}
P: 2 L: 4 UN: {} CH: {}
STEP: 18
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}
P: 2 L: 4 UN: {} CH: {}
STEP: 19
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}
P: 2 L: 4 UN: {} CH: {}
STEP: 20
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}
P: 2 L: 4 UN: {} CH: {}
DONE
Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}
P: 2 L: 4 UN: {} CH: {}
Result