I have been going through Breadth First Traversal at this link
Breadth First Traversal
Now what if the graph structure is changed to this
The node 3 is now disconnected from the graph.
When traversal program is now used, it does'nt display vertex 3.
Is there a way where we can dispaly this vertex as well?
To my understanding, BFS would keep looking for unvisited nodes as long as they exist; however, if this is not done, BFS only visits nodes in the connected component of the initial vertex. This seems to be more a matter of definition than an actual programming problem; simply restart the BFS implementation on unvisited nodes as long as they exist - if visiting of all connected components is desired.
Many implementations of BFS/DFS assume implicitly that the graph is connected.
Is there a way where we can dispaly this vertex as well?
Yes there is. If after finishing BFS still there are some unvisited vertices, enqueue them into the queue.
If you have a list of all nodes, your graph search algorithm of choice (DFS/BFS) will discover the connected components one at a time.
You could do this in the following way.
For example, consider your example graph in which there are 4 nodes and edges between 0, 2, 2, 0 and 1, 2 and node 3 has no incoming or outgoing edges.
You would have a list of nodes {0, 1, 2, 3}
And to discover all connected components, you would do the following:
Initialize visited array. Set all nodes to false
for node in list:
if not visited: dfs(node)
where dfs is implemented in the usual way. Here when you run the code on our list {0,1,2,3}, the nodes {0,1,2} will be visited by the first dfs call and 0,1,2 will be marked visited. Then when we come across 3, since it is not visited, there will be another dfs call.
Hope you get the idea.
Related
Background
Looking at the below image, we're facing an issue with how we want to do our limiting at a breadth-level.
The goal is to ensure that off each neighbor, we never read more than X edges off the current node to avoid timeouts on nodes with a large amount of edges.
Example
We have a max-breadth limit of X where X is the number of neighbors we aggregate off a single node. We begin a BFS traversal from 0 and aggregate 3, 1 and 2.
Assuming our max-breath limit is 3, the problem that can occur is that we first pull 1 and immediately begin reading all of 1's neighbors. As a result, we completely disregard the neighbors that could exist off of nodes 3 and 2 because 1 would fulfill the max-breadth.
Question
How can we, in Gremlin (in a single query), say that we want an edge limit for each node in my list of neighbors?
In other words, I want X neighbors from node 3, X neighbors from node 1 and X neighbors from node 2. This idea should hold true recursively up until some depth D.
Attempt
g.V(idsList).outE().limit(50).inV().dedup.by(T.id).fold()
The issue with the above is that we blindly limit all edges from all neighbors to X which can favor a single subgraph.
I am given a Directed Graph that is not weighted. If traveling only with the direction of the edges, if I am given a vertex I want to know if every other vertex is reachable. If the graph is a Complete Graph this is obvious. I'm interested in the case where the graph is incomplete.
As far as implementation I store each connection in a multimap. The multimap key edge tail the multimap value is the edge head. So say that I have the following pairs:
(1, 2)
(2, 3)
(1, 4)
In this graph if 1 was the given node, each node can be directly or indirectly reached. If another pair was added to the multimap: (5, 3) 5 would not be directly or indirectly reachable from 1, nor would any node but 3 be reachable from 5, so no given node in this graph would be able to reach all other nodes.
My question is this: If all I'm doing with this graph is testing if a single node can reach all other nodes, should I add edges to the multimap to make all indirect connections direct and then check if all nodes are connected to the given node? Or is there a better way to do this?
So you're using the multimap as an adjacency list? Are you asking us if you should insert adjacencies in this list if two nodes can reach each other? I would strongly recommend against such an approach. If later you want to perform any graph traversal, your structure will be polluted with edges that aren't really there.
If you really need to memoize such information, use a separate structure for reachability.
I'm new to graph theory and have been solving a few problems lately.
I was looking for a way to find the depth of every node in the graph. Here's how I formalized it on paper:
Run a for loop iterating over every vertex
If the vertex is not visited, run a DFS with that vertex as a source
In the DFS, as long as we have more vertices to go to, we keep going (as in the Depth First Search) and we keep a counter, cnt1, which increments every time
While backtracking in the recursive DFS call, we initialize a new counter starting from the current count at the last vertex, and give the value cnt1-cnt2 to each vertex so on and keep decreasing cnt2.
I'm not sure if this is correct and am not able to implement the same in code. Any suggestions? For the record, my graph is stored in the form of an adjacency list, in the form of an array of vectors like:
vector <int> a[100];
EDIT:The input graph is a collection of directed trees. We have a depth label for each node - denoting the number of nodes on the simple path from the root to it. Hence we need to find the maximum depth of each node
You may find these links helpful:
http://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/
http://www.geeksforgeeks.org/depth-first-traversal-for-a-graph/
Here classes are used to implement BFS/DFS using an adjacency list representation.Just like the array 'visited' of bool type used here...you have to also create another array 'depth' in wich you can store the depth of each element while computation..and then output that array in the end..
I have a large graph which represents a set of dependencies.
A -->B
A -->C
B -->F
C -->G
D -->E
E -->L
F -->H
F -->I
H -->K
J -->K
Given B as a starting node, result needs to be : B F I H K
I do not need any other nodes since they cannot be reached from B.
A user can specify a node and I need to collect all the paths and the nodes in those paths and print the topological sort of those nodes.
Currently I am implementing this by
Create a new graph
Extract all the outgoing edges from the given node.
Add those edges in a graph. From those edges, get the nodes and also add them to the new graph.
For each node in step 3, repeat (2) and (3)
On that new graph, perform a topological sort and get the order of the nodes
Is there any other better way to do this or known algorithm for finding the topological sort of a subset of nodes?
I would look into the depth_first_visit() function, and the Visitor Pattern provided by boost. The Visitor Pattern allows you to inspect vertices and edges as the tree is searched. The depth_first_visit() function allows you to start a DFS search starting at a specific vertex.
I am representing my graph as a adjacency list. I want to know how can I find a cluster of nodes which are internally connected but no edge points outwards from them. Is there any well known algorithm out there which I can use?
for e.g.This is my graph.
1---->2
2---->1
2---->3
3---->1
3---->4
4---->5
5---->4
Here nodes 4 and 5 are internally connected. Yet no outside edge comes from this. This would be my answer. Similarly nodes 1,2,3 even though they form a cycle, do not fit the criteria as an outside edge emanates from node 3.
So it is not same as finding a cycle in a adjacency list.
Optional read: (why I need this)
I am working on a Ranking page (search engine) algorithm, nodes like 4 and 5 are called rank-sink.
You could detect strongly connected components using Kosaraju, Tarjan or Cheriyan-Mehldorn/Gabow algorithm.
After finding these components, you compress each strongly connected components into one single node (i.e. you represent a whole component by a single node).
In the resulting graph, you look for nodes with no outgoing edges. These nodes represent the components you are interested in.