|
| 1 | +from collections import deque |
| 2 | +import random as rand |
| 3 | +import math as math |
| 4 | + |
| 5 | +# the dfault weight is 1 if not assigend but all the implementation is weighted |
| 6 | + |
| 7 | +class DirectedGraph: |
| 8 | + # enter True or False for this constructor |
| 9 | + def __init__(self): |
| 10 | + self.graph = {} |
| 11 | + |
| 12 | + # adding vertices and edges |
| 13 | + # note that self loops are not supported in undirected simpl graphs but it is in multigraphs |
| 14 | + def add_pair(self, u, v, w = 1): |
| 15 | + if self.graph.get(u): |
| 16 | + if self.graph[u].count([w,v]) == 0: |
| 17 | + self.graph[u].append([w, v]) |
| 18 | + else: |
| 19 | + self.graph[u] = [[w, v]] |
| 20 | + if not self.graph.get(v): |
| 21 | + self.graph[v] = [] |
| 22 | + def remove_pair(self, u, v): |
| 23 | + if self.graph.get(u): |
| 24 | + for _ in self.graph[u]: |
| 25 | + if _[1] == v: |
| 26 | + self.graph[u].remove(_) |
| 27 | + |
| 28 | + # if no destination is meant the defaut value is -1 |
| 29 | + def dfs(self, s = -2, d = -1): |
| 30 | + if s == d: |
| 31 | + return [] |
| 32 | + stack = [] |
| 33 | + visited = [] |
| 34 | + if s == -2: |
| 35 | + s = list(self.graph.keys())[0] |
| 36 | + stack.append(s) |
| 37 | + visited.append(s) |
| 38 | + ss = s |
| 39 | + |
| 40 | + while True: |
| 41 | + # check if there is any non isolated nodes |
| 42 | + if len(self.graph[s]) != 0: |
| 43 | + ss = s |
| 44 | + for __ in self.graph[s]: |
| 45 | + if visited.count(__[1]) < 1: |
| 46 | + if __[1] == d: |
| 47 | + visited.append(d) |
| 48 | + return visited |
| 49 | + else: |
| 50 | + stack.append(__[1]) |
| 51 | + visited.append(__[1]) |
| 52 | + ss =__[1] |
| 53 | + break |
| 54 | + |
| 55 | + # check if all the children are visited |
| 56 | + if s == ss : |
| 57 | + stack.pop() |
| 58 | + if len(stack) != 0: |
| 59 | + s = stack[len(stack) - 1] |
| 60 | + else: |
| 61 | + s = ss |
| 62 | + |
| 63 | + # check if se have reached the starting point |
| 64 | + if len(stack) == 0: |
| 65 | + return visited |
| 66 | + |
| 67 | + # c is the count of nodes you want and if you leave it or pass -1 to the funtion the count |
| 68 | + # will be random from 10 to 10000 |
| 69 | + def fill_graph_randomly(self, c = -1): |
| 70 | + if c == -1: |
| 71 | + c = (math.floor(rand.random() * 10000)) + 10 |
| 72 | + for _ in range(c): |
| 73 | + # every vertex has max 100 edges |
| 74 | + e = math.floor(rand.random() * 102) + 1 |
| 75 | + for __ in range(e): |
| 76 | + n = math.floor(rand.random() * (c)) + 1 |
| 77 | + if n == _: |
| 78 | + continue |
| 79 | + self.add_pair(_, n, 1) |
| 80 | + |
| 81 | + def bfs(self, s = -2): |
| 82 | + d = deque() |
| 83 | + visited = [] |
| 84 | + if s == -2: |
| 85 | + s = list(self.graph.keys())[0] |
| 86 | + d.append(s) |
| 87 | + visited.append(s) |
| 88 | + while d: |
| 89 | + s = d.popleft() |
| 90 | + if len(self.graph[s]) != 0: |
| 91 | + for __ in self.graph[s]: |
| 92 | + if visited.count(__[1]) < 1: |
| 93 | + d.append(__[1]) |
| 94 | + visited.append(__[1]) |
| 95 | + return visited |
0 commit comments