Numerous fixes to directed_and_undirected_(weighted)_graph.py (#2182)

* Numerous fixes to directed_and_undirected_(weighted)_graph.py

* dict.keys() is almost never need in modern Python
This commit is contained in:
Christian Clauss 2020-07-06 19:31:04 +02:00 committed by GitHub
parent 5f4da5d616
commit 2c75a7b3dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
import math as math
import random as rand
import time
from collections import deque from collections import deque
from math import floor
from random import random
from time import time
# the default weight is 1 if not assigned but all the implementation is weighted # the default weight is 1 if not assigned but all the implementation is weighted
@ -39,7 +39,7 @@ class DirectedGraph:
stack = [] stack = []
visited = [] visited = []
if s == -2: if s == -2:
s = list(self.graph.keys())[0] s = list(self.graph)[0]
stack.append(s) stack.append(s)
visited.append(s) visited.append(s)
ss = s ss = s
@ -48,15 +48,15 @@ class DirectedGraph:
# check if there is any non isolated nodes # check if there is any non isolated nodes
if len(self.graph[s]) != 0: if len(self.graph[s]) != 0:
ss = s ss = s
for __ in self.graph[s]: for node in self.graph[s]:
if visited.count(__[1]) < 1: if visited.count(node[1]) < 1:
if __[1] == d: if node[1] == d:
visited.append(d) visited.append(d)
return visited return visited
else: else:
stack.append(__[1]) stack.append(node[1])
visited.append(__[1]) visited.append(node[1])
ss = __[1] ss = node[1]
break break
# check if all the children are visited # check if all the children are visited
@ -75,37 +75,35 @@ class DirectedGraph:
# the count will be random from 10 to 10000 # the count will be random from 10 to 10000
def fill_graph_randomly(self, c=-1): def fill_graph_randomly(self, c=-1):
if c == -1: if c == -1:
c = (math.floor(rand.random() * 10000)) + 10 c = floor(random() * 10000) + 10
for _ in range(c): for i in range(c):
# every vertex has max 100 edges # every vertex has max 100 edges
e = math.floor(rand.random() * 102) + 1 for _ in range(floor(random() * 102) + 1):
for __ in range(e): n = floor(random() * c) + 1
n = math.floor(rand.random() * (c)) + 1 if n != i:
if n == _: self.add_pair(i, n, 1)
continue
self.add_pair(_, n, 1)
def bfs(self, s=-2): def bfs(self, s=-2):
d = deque() d = deque()
visited = [] visited = []
if s == -2: if s == -2:
s = list(self.graph.keys())[0] s = list(self.graph)[0]
d.append(s) d.append(s)
visited.append(s) visited.append(s)
while d: while d:
s = d.popleft() s = d.popleft()
if len(self.graph[s]) != 0: if len(self.graph[s]) != 0:
for __ in self.graph[s]: for node in self.graph[s]:
if visited.count(__[1]) < 1: if visited.count(node[1]) < 1:
d.append(__[1]) d.append(node[1])
visited.append(__[1]) visited.append(node[1])
return visited return visited
def in_degree(self, u): def in_degree(self, u):
count = 0 count = 0
for _ in self.graph: for x in self.graph:
for __ in self.graph[_]: for y in self.graph[x]:
if __[1] == u: if y[1] == u:
count += 1 count += 1
return count return count
@ -116,7 +114,7 @@ class DirectedGraph:
stack = [] stack = []
visited = [] visited = []
if s == -2: if s == -2:
s = list(self.graph.keys())[0] s = list(self.graph)[0]
stack.append(s) stack.append(s)
visited.append(s) visited.append(s)
ss = s ss = s
@ -126,11 +124,11 @@ class DirectedGraph:
# check if there is any non isolated nodes # check if there is any non isolated nodes
if len(self.graph[s]) != 0: if len(self.graph[s]) != 0:
ss = s ss = s
for __ in self.graph[s]: for node in self.graph[s]:
if visited.count(__[1]) < 1: if visited.count(node[1]) < 1:
stack.append(__[1]) stack.append(node[1])
visited.append(__[1]) visited.append(node[1])
ss = __[1] ss = node[1]
break break
# check if all the children are visited # check if all the children are visited
@ -148,7 +146,7 @@ class DirectedGraph:
def cycle_nodes(self): def cycle_nodes(self):
stack = [] stack = []
visited = [] visited = []
s = list(self.graph.keys())[0] s = list(self.graph)[0]
stack.append(s) stack.append(s)
visited.append(s) visited.append(s)
parent = -2 parent = -2
@ -161,25 +159,25 @@ class DirectedGraph:
# check if there is any non isolated nodes # check if there is any non isolated nodes
if len(self.graph[s]) != 0: if len(self.graph[s]) != 0:
ss = s ss = s
for __ in self.graph[s]: for node in self.graph[s]:
if ( if (
visited.count(__[1]) > 0 visited.count(node[1]) > 0
and __[1] != parent and node[1] != parent
and indirect_parents.count(__[1]) > 0 and indirect_parents.count(node[1]) > 0
and not on_the_way_back and not on_the_way_back
): ):
len_stack = len(stack) - 1 len_stack = len(stack) - 1
while True and len_stack >= 0: while True and len_stack >= 0:
if stack[len_stack] == __[1]: if stack[len_stack] == node[1]:
anticipating_nodes.add(__[1]) anticipating_nodes.add(node[1])
break break
else: else:
anticipating_nodes.add(stack[len_stack]) anticipating_nodes.add(stack[len_stack])
len_stack -= 1 len_stack -= 1
if visited.count(__[1]) < 1: if visited.count(node[1]) < 1:
stack.append(__[1]) stack.append(node[1])
visited.append(__[1]) visited.append(node[1])
ss = __[1] ss = node[1]
break break
# check if all the children are visited # check if all the children are visited
@ -201,7 +199,7 @@ class DirectedGraph:
def has_cycle(self): def has_cycle(self):
stack = [] stack = []
visited = [] visited = []
s = list(self.graph.keys())[0] s = list(self.graph)[0]
stack.append(s) stack.append(s)
visited.append(s) visited.append(s)
parent = -2 parent = -2
@ -214,26 +212,26 @@ class DirectedGraph:
# check if there is any non isolated nodes # check if there is any non isolated nodes
if len(self.graph[s]) != 0: if len(self.graph[s]) != 0:
ss = s ss = s
for __ in self.graph[s]: for node in self.graph[s]:
if ( if (
visited.count(__[1]) > 0 visited.count(node[1]) > 0
and __[1] != parent and node[1] != parent
and indirect_parents.count(__[1]) > 0 and indirect_parents.count(node[1]) > 0
and not on_the_way_back and not on_the_way_back
): ):
len_stack_minus_one = len(stack) - 1 len_stack_minus_one = len(stack) - 1
while True and len_stack_minus_one >= 0: while True and len_stack_minus_one >= 0:
if stack[len_stack_minus_one] == __[1]: if stack[len_stack_minus_one] == node[1]:
anticipating_nodes.add(__[1]) anticipating_nodes.add(node[1])
break break
else: else:
return True return True
anticipating_nodes.add(stack[len_stack_minus_one]) anticipating_nodes.add(stack[len_stack_minus_one])
len_stack_minus_one -= 1 len_stack_minus_one -= 1
if visited.count(__[1]) < 1: if visited.count(node[1]) < 1:
stack.append(__[1]) stack.append(node[1])
visited.append(__[1]) visited.append(node[1])
ss = __[1] ss = node[1]
break break
# check if all the children are visited # check if all the children are visited
@ -253,15 +251,15 @@ class DirectedGraph:
return False return False
def dfs_time(self, s=-2, e=-1): def dfs_time(self, s=-2, e=-1):
begin = time.time() begin = time()
self.dfs(s, e) self.dfs(s, e)
end = time.time() end = time()
return end - begin return end - begin
def bfs_time(self, s=-2): def bfs_time(self, s=-2):
begin = time.time() begin = time()
self.bfs(s) self.bfs(s)
end = time.time() end = time()
return end - begin return end - begin
@ -309,7 +307,7 @@ class Graph:
stack = [] stack = []
visited = [] visited = []
if s == -2: if s == -2:
s = list(self.graph.keys())[0] s = list(self.graph)[0]
stack.append(s) stack.append(s)
visited.append(s) visited.append(s)
ss = s ss = s
@ -318,15 +316,15 @@ class Graph:
# check if there is any non isolated nodes # check if there is any non isolated nodes
if len(self.graph[s]) != 0: if len(self.graph[s]) != 0:
ss = s ss = s
for __ in self.graph[s]: for node in self.graph[s]:
if visited.count(__[1]) < 1: if visited.count(node[1]) < 1:
if __[1] == d: if node[1] == d:
visited.append(d) visited.append(d)
return visited return visited
else: else:
stack.append(__[1]) stack.append(node[1])
visited.append(__[1]) visited.append(node[1])
ss = __[1] ss = node[1]
break break
# check if all the children are visited # check if all the children are visited
@ -345,30 +343,28 @@ class Graph:
# the count will be random from 10 to 10000 # the count will be random from 10 to 10000
def fill_graph_randomly(self, c=-1): def fill_graph_randomly(self, c=-1):
if c == -1: if c == -1:
c = (math.floor(rand.random() * 10000)) + 10 c = floor(random() * 10000) + 10
for _ in range(c): for i in range(c):
# every vertex has max 100 edges # every vertex has max 100 edges
e = math.floor(rand.random() * 102) + 1 for _ in range(floor(random() * 102) + 1):
for __ in range(e): n = floor(random() * c) + 1
n = math.floor(rand.random() * (c)) + 1 if n != i:
if n == _: self.add_pair(i, n, 1)
continue
self.add_pair(_, n, 1)
def bfs(self, s=-2): def bfs(self, s=-2):
d = deque() d = deque()
visited = [] visited = []
if s == -2: if s == -2:
s = list(self.graph.keys())[0] s = list(self.graph)[0]
d.append(s) d.append(s)
visited.append(s) visited.append(s)
while d: while d:
s = d.popleft() s = d.popleft()
if len(self.graph[s]) != 0: if len(self.graph[s]) != 0:
for __ in self.graph[s]: for node in self.graph[s]:
if visited.count(__[1]) < 1: if visited.count(node[1]) < 1:
d.append(__[1]) d.append(node[1])
visited.append(__[1]) visited.append(node[1])
return visited return visited
def degree(self, u): def degree(self, u):
@ -377,7 +373,7 @@ class Graph:
def cycle_nodes(self): def cycle_nodes(self):
stack = [] stack = []
visited = [] visited = []
s = list(self.graph.keys())[0] s = list(self.graph)[0]
stack.append(s) stack.append(s)
visited.append(s) visited.append(s)
parent = -2 parent = -2
@ -390,25 +386,25 @@ class Graph:
# check if there is any non isolated nodes # check if there is any non isolated nodes
if len(self.graph[s]) != 0: if len(self.graph[s]) != 0:
ss = s ss = s
for __ in self.graph[s]: for node in self.graph[s]:
if ( if (
visited.count(__[1]) > 0 visited.count(node[1]) > 0
and __[1] != parent and node[1] != parent
and indirect_parents.count(__[1]) > 0 and indirect_parents.count(node[1]) > 0
and not on_the_way_back and not on_the_way_back
): ):
len_stack = len(stack) - 1 len_stack = len(stack) - 1
while True and len_stack >= 0: while True and len_stack >= 0:
if stack[len_stack] == __[1]: if stack[len_stack] == node[1]:
anticipating_nodes.add(__[1]) anticipating_nodes.add(node[1])
break break
else: else:
anticipating_nodes.add(stack[len_stack]) anticipating_nodes.add(stack[len_stack])
len_stack -= 1 len_stack -= 1
if visited.count(__[1]) < 1: if visited.count(node[1]) < 1:
stack.append(__[1]) stack.append(node[1])
visited.append(__[1]) visited.append(node[1])
ss = __[1] ss = node[1]
break break
# check if all the children are visited # check if all the children are visited
@ -430,7 +426,7 @@ class Graph:
def has_cycle(self): def has_cycle(self):
stack = [] stack = []
visited = [] visited = []
s = list(self.graph.keys())[0] s = list(self.graph)[0]
stack.append(s) stack.append(s)
visited.append(s) visited.append(s)
parent = -2 parent = -2
@ -443,26 +439,26 @@ class Graph:
# check if there is any non isolated nodes # check if there is any non isolated nodes
if len(self.graph[s]) != 0: if len(self.graph[s]) != 0:
ss = s ss = s
for __ in self.graph[s]: for node in self.graph[s]:
if ( if (
visited.count(__[1]) > 0 visited.count(node[1]) > 0
and __[1] != parent and node[1] != parent
and indirect_parents.count(__[1]) > 0 and indirect_parents.count(node[1]) > 0
and not on_the_way_back and not on_the_way_back
): ):
len_stack_minus_one = len(stack) - 1 len_stack_minus_one = len(stack) - 1
while True and len_stack_minus_one >= 0: while True and len_stack_minus_one >= 0:
if stack[len_stack_minus_one] == __[1]: if stack[len_stack_minus_one] == node[1]:
anticipating_nodes.add(__[1]) anticipating_nodes.add(node[1])
break break
else: else:
return True return True
anticipating_nodes.add(stack[len_stack_minus_one]) anticipating_nodes.add(stack[len_stack_minus_one])
len_stack_minus_one -= 1 len_stack_minus_one -= 1
if visited.count(__[1]) < 1: if visited.count(node[1]) < 1:
stack.append(__[1]) stack.append(node[1])
visited.append(__[1]) visited.append(node[1])
ss = __[1] ss = node[1]
break break
# check if all the children are visited # check if all the children are visited
@ -485,13 +481,13 @@ class Graph:
return list(self.graph) return list(self.graph)
def dfs_time(self, s=-2, e=-1): def dfs_time(self, s=-2, e=-1):
begin = time.time() begin = time()
self.dfs(s, e) self.dfs(s, e)
end = time.time() end = time()
return end - begin return end - begin
def bfs_time(self, s=-2): def bfs_time(self, s=-2):
begin = time.time() begin = time()
self.bfs(s) self.bfs(s)
end = time.time() end = time()
return end - begin return end - begin