# Tag Info

## New answers tagged graph

3

First things first: while nloops<=llno: if __name__ == '__main__': would make more sense with the order reversed; and def f(l1,l2, q, r, s): ... q.put(distance) r.put((node1a, node1b)) s.put((node2a, node2b)) while nloops<=llno: if __name__ == '__main__': q = Queue() r = Queue() s = Queue() ...

2

Firstly, use a PEP8 checker. It will raise a lot of issues about whitespace and also tell you to change if visited[k[0]-1] == False: to either if visited[k[0]-1] is False: or if not visited[k[0]-1]: The DFS is far more complicated than necessary: We don't need visited: given that we know that the graph is a tree, it ...

2

Ohhh. Don't do this: using EdgeNodes = vector<EdgeNode *>; unordered_map<char, EdgeNodes *> adj; Now you need to manage the memory of the EdgeNodes. Simply declare it as a value type: using EdgeNodes = vector<EdgeNode>; // Remove the star unordered_map<char, EdgeNodes> adj; // Remove the star. To make the rest of your ...

3

for p in permutations(all_vertices, v): $1≤n≤10^5$ Well, $(10^5)! \approx \left(\frac{10^5}{e}\right)^{10^5} \approx 10^{35657}$ so it's a waste of time trying to optimise this. The only thing to do is go back to the drawing board and spend a few hours thinking about the mathematics. At best the code you've written will serve to analyse all ...

1

Wikipedia contains circular references. Currently you follow them around until your max depth is reached. Instead, keep a set of all sites visited so far. You also don't need this function to be recursive, you can make it iterative like this: from collections import deque def build_link_graph(start, max_pages=10): queue = deque([start]) visited = ...

Top 50 recent answers are included