-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadGraph.py
49 lines (37 loc) · 1.02 KB
/
LoadGraph.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
import networkx as nx
import os.path
def load_graph(path, weighted=False, delimiter='\t', self_loop=False):
graph = nx.Graph()
if not os.path.isfile(path):
print("Error: file " + path + " not found!")
exit(-1)
with open(path) as file:
for line in file.readlines():
w = 1.0
line = line.split(delimiter)
v1 = int(line[0])
v2 = int(line[1])
graph.add_node(v1)
graph.add_node(v2)
if weighted:
w = float(line[2])
if (self_loop and v1 == v2) or (v1 != v2):
graph.add_edge(v1, v2, weight=w)
return graph
def load_graph_uncertain(path, delimiter='\t', self_loop=False):
graph = nx.Graph()
if not os.path.isfile(path):
print("Error: file " + path + " not found!")
exit(-1)
with open(path) as file:
for line in file.readlines():
line = line.split(delimiter)
v1 = int(line[0])
v2 = int(line[1])
graph.add_node(v1)
graph.add_node(v2)
w = float(line[2])
p = float(line[3])
if (self_loop and v1 == v2) or (v1 != v2):
graph.add_edge(v1, v2, weight=w, prob=p)
return graph