Você está na página 1de 33

Building Spanning Tree and Leader selection

Bar-Ilan Shmuel Goldklang 2007

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:

upon receiving no message: if parent = NULL then leader := id parent := Pi explore()

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:

upon receiving no message: if parent = NULL then leader := id parent := Pi explore()

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:

upon receiving no message: if parent = NULL then leader := id parent := Pi explore()

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:

upon receiving no message: if parent = NULL then leader := id parent := Pi explore()

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:

upon receiving no message: if parent = NULL then leader := id parent := Pi explore()

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()

Handle Leader Message

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:

upon receiving no message: if parent = NULL then leader := id parent := Pi explore()

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()

Handle ALREADY & PARENT Message

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: {}

P: 3 L: 3 UN: {2} CH: {}

P: 4 L: 4 UN: {1,2} 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: {}

P: 4 L: 4 UN: {1} 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: {}

P: 4 L: 4 UN: {1} 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: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 4

Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {1} CH: {}

P: 3 L: 3 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 5

Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {1} CH: {}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 6

Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {1} CH: {}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} 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: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 8

Example
P: 1 L: 1 UN: {2} CH: {} P: 4 L: 4 UN: {} CH: {3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 9

Example
P: 2 L: 4 UN: {4} CH: {} P: 4 L: 4 UN: {} CH: {3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 10

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 11

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 12

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 13

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 14

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {}

STEP: 15

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {1} CH: {2}

STEP: 16

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {} CH: {2}

STEP: 17

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {} CH: {2}

STEP: 18

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {} CH: {2}

STEP: 19

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {} CH: {2}

STEP: 20

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {} CH: {2}

DONE

Example
P: 2 L: 4 UN: {} CH: {} P: 4 L: 4 UN: {} CH: {1,3}

P: 2 L: 4 UN: {} CH: {}

P: 4 L: 4 UN: {} CH: {2}

Result

Você também pode gostar