-
Notifications
You must be signed in to change notification settings - Fork 3
/
connected_components_undirected_graph.py
60 lines (54 loc) · 1.75 KB
/
connected_components_undirected_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
# by Johannes Staib TIN17IN
# Algorithmus von http://www.inf.fh-flensburg.de/lang/algorithmen/graph/connected-components.htm
from tkinter import Tk
try: import tkinter.filedialog as filedialog # Py3
except ImportError: import tkFileDialog as filedialog # Py2
import re
class Vertex:
c = 0
vertexes = {}
edges = []
def __init__(self,nr):
self.nr = nr
self.marked = 0
def neighbours(self):
for e in Vertex.edges:
if self.nr in e:
temp = list(e)
temp
yield Vertex.vertexes[temp.pop()]
def execute(self):
self.marked = Vertex.c
for n in self.neighbours():
if n.marked == 0:
n.execute()
pass
@staticmethod
def find():
for v in Vertex.vertexes.values():
if v.marked == 0 :
Vertex.c = Vertex.c+1
v.execute()
pass
@staticmethod
def output():
for i in range(1,Vertex.c+1):
print(" ".join([ str(v.nr) for v in Vertex.vertexes.values() if v.marked == i ]))
pass
Tk().withdraw()
fpath = filedialog.askopenfilename()
if fpath:
with open(fpath,"r") as fh:
# read edges
for line in fh.readlines():
line = [ int(n) for n in re.findall(r'[0-9]+',line)] # fine vertex numbers in each line
# create vertexes
for vertexNr in line:
if not vertexNr in Vertex.vertexes.keys():
Vertex.vertexes[vertexNr] = Vertex(vertexNr)
# add edge if exist in line
if len(line) == 2:
Vertex.edges.append((line[0],line[1]))
# find connected component
Vertex.find()
Vertex.output()