Skip to content
Snippets Groups Projects
Commit 466eb4d7 authored by Jacob Theisen's avatar Jacob Theisen
Browse files

fiks ov6

parent d8da3841
Branches
No related tags found
No related merge requests found
ov6/L7g2 0 → 100644
50 100
36 33
15 27
35 43
42 36
21 49
27 12
9 40
26 13
26 40
36 22
18 11
29 17
30 32
23 12
35 17
2 29
8 22
17 19
6 43
42 11
23 29
19 21
37 34
24 48
20 15
26 13
30 41
23 6
20 12
31 46
25 5
27 34
5 36
29 46
7 13
45 24
45 32
17 14
14 34
0 43
8 37
28 26
34 38
1 3
49 4
10 32
18 26
12 39
36 26
39 44
20 45
28 34
1 17
2 47
42 17
6 2
30 1
41 36
39 15
19 44
29 40
17 31
21 47
25 31
27 9
6 17
3 47
15 36
33 6
24 19
21 28
29 32
19 3
18 20
15 8
49 40
23 46
45 18
1 46
5 21
38 29
28 14
0 41
0 43
14 34
14 24
6 37
41 43
15 27
36 9
1 32
28 37
7 25
21 24
45 8
37 29
43 35
28 18
11 43
29 28
...@@ -10,4 +10,3 @@ ...@@ -10,4 +10,3 @@
2 5 2 5
6 3 6 3
2 4 2 4
#prep and read file #prep and read file
with open("./fil.txt") as file: with open("./fil.txt") as file:
node_file_unsplit = file.read() node_file_unsplit = file.read()
...@@ -10,81 +11,65 @@ node_file.pop(0) and node_file.pop(0) ...@@ -10,81 +11,65 @@ node_file.pop(0) and node_file.pop(0)
#prep done #prep done
class Con(object):
def __init__(self, node, to_node):
self.node = node
self.to_node = to_node
class NodeTable(object): class NodeTable(object):
def __init__(self): def __init__(self):
self.all_cons = [] self.all_cons = []
self.queue = []
self.finished_nodes = [] self.finished_nodes = []
def add_all(self, con): def add_all(self, node, to_node):
self.all_cons.append(int(con)) self.all_cons.append(Con(int(node), int(to_node)))
def calculate(self, start): def calculate(self, start):
#add startvarable to queue while True:
if start not in self.queue: #check if node is 'fra node', if so return this tuple, if not, return -1
self.queue.append(start) a = next((v for v in self.all_cons if v.node==start),-1)
#loop while still elements in queue
while self.queue:
node_alr_in_queue = 0
node_alr_fini = 0
#check if there is matching elemtens with the startvariable , with even indexes,
#if so retrun this index, if not, return -1
a = next((i for i,v in enumerate(self.all_cons) if (i%2 == 0 and v==start)),-1)
if a == -1: if a == -1:
#if no matching elements and first element in queue #check if node is 'til node', if so return tuple, if not return -1
if start == self.queue[0]: b = next((v for v in self.all_cons if v.to_node==start),-1)
#check if there is matching elemtens with the startvariable, with odd indexes,
#if so retrun this index, if not, return -1
b = next((i for i,v in enumerate(self.all_cons) if (i%2 == 1 and v==start)),-1)
if b == -1: if b == -1:
#if no even or odd indexes mathing elemnts return finished array #if not 'til node' and finished_nodes is not empty, means were on top of graph
if self.finished_nodes:
#if theres still cons left, as sister on top of graph, add these to finished nodes
if self.all_cons:
for i in self.all_cons:
self.finished_nodes.insert(0, i.node)
self.finished_nodes.insert(0, start) self.finished_nodes.insert(0, start)
return self.finished_nodes #print result and exit program
#if odd index, pop conection between matching node and its neighbour, print(self.finished_nodes)
# and call function recursively on its neighbour exit()
elif b != -1: #if not 'fra node' or 'til node', and finished array is empty, node is not in graph
self.all_cons.pop(b) else:
self.all_cons.pop(b-1) raise(ValueError("Node is not in graph??"))
self.calculate(b) #if 'til node' insert node to finished node, remove con from all_cons and call function on parent node
#if no matching elemens and not first element in queue, add node to finished nodes array else:
#pop queue and jump to its parent(last element in queue after pop)
if start != self.queue[0] and a == -1:
print(self.queue)
print("fini", self.finished_nodes)
self.finished_nodes.insert(0, start) self.finished_nodes.insert(0, start)
self.queue.pop() self.all_cons.remove(b)
self.calculate(self.queue[len(self.queue)-2]) nt.calculate(b.node)
#if matching element #if 'fra node'
elif a != -1: else:
neighbour = self.all_cons[a+1] #if not finished call func calculate on child
#if already in queue if a.to_node not in self.finished_nodes:
for i in self.queue: nt.calculate(a.to_node)
if neighbour == i: #if finished, remove con from all_cons
node_alr_in_queue = 1 else:
#if already finished self.all_cons.remove(a)
for j in self.finished_nodes:
if neighbour == j:
node_alr_fini = 1
#if not in queue and not finished, pop conection between node and neighbour and call function recursively
#on neighbour
if node_alr_fini == 0 and node_alr_in_queue == 0:
#print(self.all_cons[a], self.all_cons[a+1])
self.all_cons.pop(a)
self.all_cons.pop(a)
self.calculate(neighbour)
#if already in queue or finished, pop conection
elif node_alr_fini == 1 or node_alr_in_queue == 1:
self.all_cons.pop(a)
self.all_cons.pop(a)
return self.finished_nodes
#init things #init things
nt = NodeTable() nt = NodeTable()
for e in node_file: for i in range(0,len(node_file),2):
nt.add_all(e) nt.add_all(node_file[i], node_file[i+1])
start_node = int(input("Startnode: ")) start_node = int(input("Startnode: "))
a = nt.calculate(start_node) try:
#print result nt.calculate(start_node)
print(a) #if recursion error, the graph probably contains a cycle
\ No newline at end of file except RecursionError as re:
print(re)
print("Recursionerror! Does the graph contain a cycle?")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment