Skip to content

Commit

Permalink
added BFS visualization. having issues visualizing BFS right now
Browse files Browse the repository at this point in the history
  • Loading branch information
Nemo Biswas committed May 3, 2016
1 parent ce20dc2 commit 7f2b30a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 20 deletions.
40 changes: 27 additions & 13 deletions datk/core/algs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,39 @@
#Leader Election Algorithms for Ring networks:

class Leader_Election_Synchronous_Algorithm(Synchronous_Algorithm):
def draw(self,network):
vals = network.draw(node_coloring = True)
def get_draw_args(self,network,vals):
"""network - refers to the network on which the algorithm is running.
vals - the positions of the nodes in the network"""
node_colors = dict()
edge_colors = None
for p in network.processes:
v = vals[p.UID] # IMPORTANT: check to make sure this is right!
# if self.has(p, "decided"):
if p.state['status'] == "leader":
plt.plot( [v[0]], [v[1]], 'ro' )
node_colors[v] = 'ro'

elif p.state['status'] == "non-leader": # non-leader
node_colors[v] = 'bo'

# algoDrawArgs = AlgorithmDrawArgs(node_colors = node_colors, edge_colors = edge_colors)
return node_colors, edge_colors

class BFS_Synchronous_Algorithm(Synchronous_Algorithm):
def get_draw_args(self,network,vals):
"""network - refers to the network on which the algorithm is running.
vals - the positions of the nodes in the network"""
node_colors = None
edge_colors = dict()
for p in network.processes:
v = vals[p.UID]
if p.state['parent']:
v_parent = vals[p.state['parent']]
edge_colors[(v,v_parent)] = 'r'

return node_colors, edge_colors

else: # non-leader
plt.plot( [v[0]], [v[1]], 'bo' )

# else:
# plt.plot( [v[0]], [v[1]], 'go' )

plt.show()

class LCR(Leader_Election_Synchronous_Algorithm):
"""The LeLann, Chang and Roberts algorithm for Leader Election in a Synchronous Ring Network
Expand Down Expand Up @@ -349,15 +367,13 @@ class SynchVariableSpeeds(Synchronous_Algorithm):
#Construct BFS Tree
class SynchBFS(Synchronous_Algorithm):
"""Constructs a BFS tree with the 'leader' Process at its root
At any point during execution, there is some set of processes that is
"marked," initially just i0. Process i0 sends out a search message at
round 1, to all of its outgoing neighbors. At any round, if an unmarked
process receives a search message, it marks itself and chooses one of the
processes from which the search has arrived as its parent. At the first
round after a process gets marked, it sends a search message to all of its
outgoing neighbors.
Requires:
- testLeaderElection
Effects:
Expand Down Expand Up @@ -388,7 +404,6 @@ def trans_i(self, p, msgs):

class SynchBFSAck(Synchronous_Algorithm):
"""Constructs a BFS tree with children pointers and the 'leader' Process at its root
Algorithm (Informal):
At any point during execution, there is some set of processes that is
"marked," initially just i0. Process i0 sends out a search message at
Expand All @@ -398,7 +413,6 @@ class SynchBFSAck(Synchronous_Algorithm):
round after a process gets marked, it sends a search message to all of its
outgoing neighbors, and an acknowledgement to its parent, so that nodes
will also know their children.
Requires:
- testLeaderElection
Effects:
Expand Down Expand Up @@ -443,7 +457,7 @@ def trans_i(self, p, msgs):
p.terminate(self)
if self.params["verbosity"]>=Algorithm.VERBOSE:
print p,"knows children"

#Convergecast

class SynchConvergecast(Synchronous_Algorithm):
Expand Down
32 changes: 25 additions & 7 deletions datk/core/distalgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,15 @@ class Network:

def __init__(self, processes):
self.processes = processes
self.algs = []

def __init__(self, n, index_to_UID = None):
"""
Creates a network of n disconnected Processes,
with random distinct UIDs, or as specified by
the index_to_UID function
"""
self.algs = []
if index_to_UID is None:
self.processes = [Process(i) for i in range(n)]
shuffle(self.processes)
Expand All @@ -181,14 +183,15 @@ def __init__(self, n, index_to_UID = None):

def add(self, algorithm):
"""Awakens all Processes in the Network with respect to algorithm"""
self.algs.append(algorithm)
for process in self:
process.add(algorithm)

def run(self, algorithm):
"""Runs algorithm on the Network"""
algorithm(self)

def draw(self, style='spectral'):
def draw(self, style='spectral', default_node_coloring = True, default_edge_coloring = True):
"""
Draws the network
Expand Down Expand Up @@ -216,17 +219,32 @@ def draw(self, style='spectral'):
for k in range(n):
vals.append( [math.cos(2*k*math.pi/n), math.sin(2*k*math.pi/n) ] )

plt.plot( [v[0] for v in vals], [v[1] for v in vals], 'ro' )


def line(v1, v2,color='k'):
plt.plot( (v1[0], v2[0]), (v1[1], v2[1] ),color)

def line(v1, v2):
plt.plot( (v1[0], v2[0]), (v1[1], v2[1] ))
for i in range(n):
for nbr in self[i].out_nbrs:
line(vals[i], vals[self.index(nbr)])
if default_edge_coloring:
for i in range(n):
for nbr in self[i].out_nbrs:
line(vals[i], vals[self.index(nbr)])

frame = plt.gca()
frame.axes.get_xaxis().set_visible(False)
frame.axes.get_yaxis().set_visible(False)
if default_node_coloring:
plt.plot( [v[0] for v in vals], [v[1] for v in vals], 'ro' )

for alg in self.algs:
node_colors, edge_colors = alg.get_draw_args(self,vals)
if node_colors:
for node,node_color in node_colors.iteritems():
plt.plot( [node[0]], [node[1]], node_color)

if edge_colors:
for (v1,v2),edge_color in edge_colors.iteritems():
line(v1,v2,color=edge_color)

plt.show()


Expand Down

0 comments on commit 7f2b30a

Please sign in to comment.