This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 597
/
graph.py
133 lines (120 loc) · 4.1 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
''' graph.py '''
################################################################################
class Graph:
'''
Adjacency list of edges in graph. This will correspond to the streams in a topology DAG.
'''
def __init__(self):
# graph adjecency list. Implemented as a map with key = vertices and
# values = set of Vertices in adjacency list.
self.edges = {}
# pylint: disable=invalid-name
def add_edge(self, U, V):
'''
:param U:
:param V:
:return:
'''
if not U in self.edges:
self.edges[U] = set()
if not V in self.edges:
self.edges[V] = set()
if not V in self.edges[U]:
self.edges[U].add(V)
def __str__(self):
return str(self.edges)
def bfs_depth(self, U):
'''
Returns the maximum distance between any vertex and U in the connected
component containing U
:param U:
:return:
'''
bfs_queue = [[U, 0]] # Stores the vertices whose BFS hadn't been completed.
visited = set()
max_depth = 0
while bfs_queue:
[V, depth] = bfs_queue.pop()
if max_depth < depth:
max_depth = depth
visited.add(V)
adj_set = self.edges[V]
for W in adj_set:
if W not in visited:
bfs_queue.append([W, depth + 1])
return max_depth
def diameter(self):
'''
Returns the maximum distance between any vertex and U in the connected
component containing U
:return:
'''
diameter = 0
for U in self.edges:
depth = self.bfs_depth(U)
if depth > diameter:
diameter = depth
return diameter
################################################################################
class TopologyDAG(Graph):
'''
Creates graph from logical plan, a deeply nested map structure that looks
like -
{
'spouts' -> {
spout_name -> {
'outputs' -> ['stream_name' -> stream_name]
}
},
'bolts' -> {
bolt_name -> {
'outputs' -> ['stream_name'->sream_name],
'inputs' -> ['stream_name'->stream_name,
'component_name'->component_name,
'grouping'->grouping_type]
}
}
}
'''
def __init__(self, logical_plan):
Graph.__init__(self)
all_spouts = logical_plan['spouts'] # 'spouts' is required
all_bolts = dict()
if 'bolts' in logical_plan:
all_bolts = logical_plan['bolts']
stream_source = dict() # Stores the mapping from input stream to component
# Spout outputs
for spout_name in all_spouts:
for output_stream_data in all_spouts[spout_name]['outputs']:
stream_name = output_stream_data['stream_name']
stream_source[('%s,%s' % (spout_name, stream_name))] = spout_name
# Bolt outputs
for bolt_name in all_bolts:
for output_stream_data in all_bolts[bolt_name]['outputs']:
stream_name = output_stream_data['stream_name']
stream_source[('%s,%s' % (bolt_name, stream_name))] = bolt_name
# Add edges from stream_source to its destination.
for bolt_name in all_bolts:
for input_stream_data in all_bolts[bolt_name]['inputs']:
stream_name = input_stream_data['stream_name']
component_name = input_stream_data['component_name']
stream_hash = ('%s,%s' % (component_name, stream_name))
self.add_edge(stream_source[stream_hash], bolt_name)