Partitioning adjacency matrix of bipartite graph - graph

Lets say I have a graph G with its adjacency matrix A. I know that G is bipartite.
How can I split the vertices in G into the two sets that always form a bipartite graph?
Thanks!

Declare an array which of size equal to the number of vertices, setting each element to 0 initially. Then perform a depth-first search through the graph, recording the "level number" that you are on as you go. This starts at 1, and alternates between 1 and 2 with each edge traversed. For every vertex reached, assign the current level to the corresponding entry of which, and (if it was previously 0) recurse to process its children. Afterwards, all elements of which will be either 1 or 2, and which[i] indicates which set vertex i belongs to.
Intuitively, you can imagine that each traversal from parent to child in the DFS takes you "down" a level, and each traversal back takes you back "up". By the bipartite property, all vertices on even levels can be connected only to vertices on odd levels and vice versa, so labelling nodes "even" or "odd" suffices to partition them into the two sets.
If your graph contains more than one component, you will of course need a separate DFS for each component.

Related

All possible possible directed graphs in V={1,2,3}

I got this exercise :
Give an example of three different directed graphs on V = {1, 2, 3}
where the edges (1, 2) and (1, 3) exist. Write both the adjacency-list and adjacency-
matrix representation for each.
I find only this two:
G = {(1,2), (1,3)}
G = {(1,2), (1,3), (2,3)}
What I miss? Something like that is valid: G = {(1,2), (1,3), (3,2)} ?
It's a directed graph, which means all links are one-way. If you want to be able to go from 1 to 2 and from 2 to 1, you need two links, (1,2) and (2,1) - that's part of the definition of a directed graph.
With that, enumerate all possible links for a graph of 3 vertexes:
(1,2)
(2,1)
(1,3)
...
Once you enumerate all possible links in such a graph, you can pick and choose unique sets of those links to make into multiple graphs, subject to the constraints given to you by the exercise.
For instance, here are a couple graphs:
{(1,2)}
{(2,1)}
{(1,3)}
{(3,1)}
{(3,2), (2,1), (1,3)}
...
You already have two out of three requested answers and you need a third graph to complete the exercise. You need to give answers that include two provided links. Why not give as an answer a graph that has every link in it? A graph of every link must contain the two requested links, right?

Find length of longest trail in directed unweighted graph

I have a directed, unweighted, possibly cyclic graph that can contain loops and multiple duplicate edges (i.e. two edges from node 1 to node 2).
I would now like to find the length of the longest trail in this graph, i.e. the longest path that:
- uses no edge twice (but if there are multiple edges from node 1 to node 2, it can use every one of them)
- possibly visits nodes several time (i.e. it does not have to be a simple path)
In particular, is this problem NP-hard? I know that the longest simple path is NP-hard (reducing Hamiltonian Path to it) and the longest trail with edge reusal is in P (Bellman ford with weight -1 on every edge). However, with this problem, I am not quite sure and I could not find good information on it.
Although I am not completely sure, I think that this problem is NP-hard. As I understand, your question arises due to multiple edges between nodes. The graphs that has multiple edges between same nodes can be expanded to larger graphs with no multiple edges between them. Thus, a graph with multiple edges between same nodes has no difference than a graph without multiple edges.
Let me walkthrough a simple example to explain:
Let there be a graph with 3 nodes (A,B,C) and 5 edges between them (A to B, A to B, B to A, B to C, C to A)
This graph can be expanded and shown with 5 nodes and 7 edges.
Lets expand the node A to 3 different nodes (A1, A2, A3). When we adjust the edges according to previous edges, there exists 7 edges(A1 to B, A2 to B, B to A3, B to C, C to A1, C to A2, C to A3)
As a result, now we have a graph without multiple edges and can be evaluated with the help of Hamiltonian and Bellman Ford.
Hope I've at least cleared the problem a bit.

Weakly connected graph traversal from least number of nodes

I've been given the following exercise: There's an unweighted, directed, weakly connected graph with n nodes (n < 1 000 000). We want to traverse the whole graph, starting from the least number of nodes. The question is: from which nodes do I start the traversals? I couldn't find any content on this particular topic. However, I managed to come up with an algorithm, but it's not efficient enough:
I store the graph in an adjacency list (n can be too high for a two-dimensional matrix)
I start a BFS from each node i, and store the nodes it reached in x[i][...] (x = List<List<int>>)
I check whether any x[i].Count == n
I check whether any (x[i] union x[j]).Count == n
I check whether any (x[i] union x[j] union x[k]).Count == n
... So I make all possible unions of 2, 3, 4... subsets of x, and check whether its count is n.
It works all right if n is not too high, but I would need a more efficient algorithm for bigger n.
Any help is appreciated (you would make me be able to fall asleep again)! :)
Find the nodes that do not have any incoming edges. Loop over these nodes, and for each node v, begin traversing the graph. Remember which nodes you visited (by putting them in a hash table or marking them). Stop traversing when you reach a node you have already visited.
You would need an adjacency list representation, where each node has a list of incoming and a list of outgoing edges. Then do something like this:
Set nodesToVisit = emptySet;
for i=1 to n:
if incoming[i].size() == 0:
nodesToVisit.add(i)
Set visited = emptySet;
for v in nodesToVisit:
nodesToVisit.remove(v)
if(v is not in visited):
visit(v);
visited.add(v);
for u in outgoing[v]:
nodesToVisit.add(u)

Generate an OD list of nodes within n stops

I have a graph G(V,E), the number of edges is 35000 and the number of nodes is 3500,
Is there anyway I can generate a origin-destination list within n (say 4) stops for each node?
I think the function neighborhood() does exactly what you want. Set the order argument to 4 and for each vertex you'll get a vector of vertex ids for the vertices that are at most 4 steps away from it.
I figure it out:
Use the property of the adjacency matrix A, the entry in row i and column j of A^n gives the number of (directed or undirected) walks of length n from vertex i to vertex j. So for n stop, construct n matrix An, A(n-1)......A1, in which, An= A^n. Then the union of An,An-1....A1 should be the matrix that representing n stop reachable destinations for an origin.

Search common subgraph between two graphs

I have two graphs G, H labeled and I want to extract all common subgraph of two graphs, I got to a part that is:
1 - extract all the nodes that are in common, but I'm stuck on the part that includes:
2 - Step 1: Take the First vertex and store it in a set P = {first element} (which will be the set of all common subgraph), and go to 2nd if it is adjacent to the first of the two P graph G and H, we add it, and so on, but I do not know how to do it when i have more than 2
That is a NP-complete problem. See http://en.wikipedia.org/wiki/Subgraph_isomorphism_problem

Resources