Python  Graph Data Structure  How Do I Implement BreadthFirst Search to Find Reachable Vertices Correctly
By : user3058430
Date : March 29 2020, 07:55 AM
I hope this helps . Your code is way too complicated. Start with a code just implementing the fundamentals and add features gradually. In order to get you started I'll post something simple but fundamental when handling graphs. code :
from collections import deque
class fringe(object):
def __init__(self, kind= 'stack'):
f= deque()
self._p= f.append if kind is 'stack' else f.appendleft
self._f= f
def __call__(self, item):
self._p(item)
return self
def __iter__(self):
while len(self._f):
item= self._f.pop()
yield item
def __repr__(self):
return self._f.__repr__().replace('deque', 'fringe')
def paths(G, start, terminal, F= fringe()):
for node, path in F((start, [start])):
for node in G[node]:
if node is terminal:
yield path+ [terminal]
elif node not in path:
F((node, path+ [node]))
if __name__ == '__main__':
a, b, c, d, e, f= 'A', 'B', 'C', 'D', 'E', 'F'
G= {a: [b, c], b: [c, d], c: [d], d: [c], e: [f], f: [c]}
print 'All paths from:', a, 'to:', d
print 'BFS'
for path in paths(G, a, d): print path
print 'DFS'
for path in paths(G, a, d, fringe('queue')): print path
All paths from: A to: D
BFS
['A', 'C', 'D']
['A', 'B', 'D']
['A', 'B', 'C', 'D']
DFS
['A', 'B', 'D']
['A', 'C', 'D']
['A', 'B', 'C', 'D']

find the minimum number of vertices in a directed graph from which the other vertices are reachable
By : Bomber Saichan
Date : March 29 2020, 07:55 AM
will be helpful for those in need For the graph without cycle(i.e acyclic graph), it will be easy. All the nodes without incoming edges will be an optimal solution. Since all other nodes should be reachable from one of the nodes. For the graph with cycle, find strongly connected component first, then you get a acyclic graph. The method above works again.

vertex or set of vertices reachable from all other vertices in a directed graph
By : user2711220
Date : March 29 2020, 07:55 AM
this one helps. Given a directed graph how to find vertex(call it "special vertex") that can be reached from all other vertices? It is not necessary that other vertices have to be reachable from this special vertex. , I would assume the graph is acyclic (i.e. a DAG)

Find all reachable vertices in a Boost BGL graph using BFS
By : Vlad
Date : March 29 2020, 07:55 AM
will be helpful for those in need The docs say that the Buffer needs to be a queue of vertex_descriptors. You accidentally declared it to have vertex_t (the vertex property bundle) as the value type. Fix it: code :
using vertex_descriptor = boost::graph_traits<Graph_t>::vertex_descriptor;
boost::queue<vertex_descriptor> Q;
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <variant>
#include <queue>
struct node_t {
};
struct specialNode_t {
};
enum class TerminalType {
};
using vertex_t = std::variant<node_t, specialNode_t>; // structs
using edge_t = std::variant<TerminalType>; // enum
using Graph_t = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, vertex_t, edge_t>;
int main() {
Graph_t myGraph(5);
boost::default_bfs_visitor vis; // Will do my collecting visitor later...
auto indexmap = boost::get(boost::vertex_index, myGraph);
auto colormap = boost::make_vector_property_map<boost::default_color_type>(indexmap);
using vertex_descriptor = boost::graph_traits<Graph_t>::vertex_descriptor;
boost::queue<vertex_descriptor> Q;
boost::breadth_first_visit(myGraph, 0, Q, vis, colormap);
}

Find all vertices reachable from a vertex of a directed graph in time O(E + V)
By : Than Oo
Date : March 29 2020, 07:55 AM
will be helpful for those in need If you perform only one step of the DFS then it is O(1 + outdegree(v)), nevertheless, that will only get you the vertices that are reachable from v at one step, but not all the vertices that you may reach. Think of the recursion, in order to retrieve all reachable vertices you should perform another recursive DFS search for each reached vertex. In the worst case you will reach all vertices and thus you will have the sum of O(1+outdegree(v)) for each vertex, so you will traverse all the edges of the graph and get O(V+E).

