This repository was archived by the owner on Oct 23, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpython_graph.py
More file actions
193 lines (141 loc) · 5.1 KB
/
Copy pathpython_graph.py
File metadata and controls
193 lines (141 loc) · 5.1 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
from collections import defaultdict,deque
class Graph:
def __init__(self):
self.vertices= defaultdict(list)#No. of vertices
self.graph = defaultdict(list) # default dictionary to store graph
self.obrnut_graph=defaultdict(list)
self.count=0
self.komponente=defaultdict(list)
self.slabe_komponente=defaultdict(list)
self.obrnute_komponente=defaultdict(list)
self.finalne_komponente=defaultdict(list)
self.lista_posjecenih=[]
# function to add an edge to graph
def addEdge(self,u,v):
#temp = (u,v)
self.graph[u].append(v)
self.graph[v].append(u)
def print_graph(self):
for key in self.graph.keys():
#print "%s: %s" % (self.vertices[key], self.graph[key])
print "%s: %s" % (key, self.graph[key])
def read_graph_pajek(self,text):
edgeDataStarts='false';
with open(text) as file:
for line in file:
s=line.split()
if s[0]=='*edges':
break
if s[0]=='*vertices':
continue
if s[0]=='*arcs':
edgeDataStarts='true'
continue
if edgeDataStarts=='false':
self.vertices[int(s[0])] = s[1].strip('"')
self.graph[int(s[0])]=[]
else:
self.addEdge(int(s[0]),int(s[1]))
"""
if int(s[0]) not in self.__graph_dic:
self.__graph_dic[int(s[0])]=[int(s[1])]
else:
self.__graph_dic[int(s[0])].append(int(s[1]))
"""
def pronadi_komponente(self):
self.lista_posjecenih=[]
counter=0
for key in self.vertices.keys():
if key in self.lista_posjecenih:
continue
path=[]
start=[key]
path=self.iterative_dfs(start)
#path.insert(0, key)
for values in path:
if values not in self.lista_posjecenih:
self.lista_posjecenih.append(values)
else:
path.remove(values)
#print path
counter=counter+1
#if len(path)>1:
self.komponente[key]=path
def pronadi_obrnute_komponente(self):
self.lista_posjecenih = []
counter = 0
for key in self.vertices.keys():
if key in self.lista_posjecenih:
continue
path = []
start = [key]
path = self.iterative_dfs(start)
# path.insert(0, key)
for values in path:
if values not in self.lista_posjecenih:
self.lista_posjecenih.append(values)
else:
path.remove(values)
# print path
counter = counter + 1
# if len(path)>1:
self.obrnute_komponente[key]=path
def iterative_dfs(self, start):
q=deque(start)
path=[]
while q:
v=q.popleft()
if v not in path:
path.append(v)
q.extendleft(reversed(self.graph[v]))
return path
def iterative_dfss(self, start):
q=deque(start)
path=[]
while q:
v=q.popleft()
if v not in path:
path.append(v)
q.extendleft(reversed(self.obrnut_graph[v]))
return path
def ispis_komponenti(self):
#print self.count
for key in self.komponente.keys():
print "%s: %s" % (key, self.komponente[key])
def definiraj_komponente(self):
for key in self.komponente.keys():
temp=self.komponente[key]
temp2=self.obrnute_komponente[key]
temp3=[]
for values in temp:
temp3.append(values)
for values in temp2:
if values not in temp3:
temp3.append(values)
self.finalne_komponente[key]=temp3
def ispis_count(self):
temp = 0
for key in self.komponente.keys():
if len(self.finalne_komponente[key])!=0:
self.count = self.count + 1
print self.count
def ispis_svega(self):
print len(self.komponente)
def max_komponenta(self):
temp=0
for key in self.finalne_komponente.keys():
temp2=len(self.finalne_komponente[key])
if temp < temp2:
temp=temp2
return temp
if __name__ == "__main__":
g=Graph()
g.read_graph_pajek("eva.net")
#g.print_graph()
g.pronadi_komponente()
g.pronadi_obrnute_komponente
g.definiraj_komponente()
g.ispis_komponenti()
g.ispis_count()
#g.ispis_svega()
#temp=g.max_komponenta()