-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_graph.py
More file actions
48 lines (31 loc) · 1.3 KB
/
load_graph.py
File metadata and controls
48 lines (31 loc) · 1.3 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
from vertex import *
def parse_line(line): # Parses and splits each line in the graph data file.
section_split = line.split(";")
vertex_name = section_split[0].strip()
adjacent_vertices = section_split[1].strip().split(",")
# add all except empty strings
adjacent = []
for a in adjacent_vertices:
if a:
adjacent.append(a.strip())
return vertex_name, adjacent
# Creates a dictionary of Vertex objects from each line in the graph data file.
def load_graph(data_file):
vertex_dict = {}
# Read the lines in the file into a list of lines:
file_first = open(data_file, "r")
for line in file_first: # First pass
# if this is a line in the correct format:
if len(line.split(";")) == 2:
vertex_name, adjacent_vertices = parse_line(line)
vertex_dict[vertex_name] = Vertex(vertex_name, [])
file_first.close()
file_second = open(data_file, "r")
for line in file_second:
# if this is a line in the correct format:
if len(line.split(";")) == 2:
vertex_name, adjacent_vertices = parse_line(line)
for adjacent_vertex in adjacent_vertices:
vertex_dict[vertex_name].adjacent.append(vertex_dict[adjacent_vertex])
file_second.close()
return vertex_dict