Search common subgraph between two graphs - graph

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

Related

3 Coloring a given graph to a boolean expression in a Mini-Sat format

I'm working on a project where I have to read a file representing a graph. I made the input file looks like this:
b c
a c d
a b
b
So each line signifies a node. For eg, the first line is node a which is connected to b and c and the second is node b connected to a, c, and d. So I'm not really sure where to start here. I know that we need a bunch of if and else statements such that two nodes sharing an edge cannot have the same color and each node should have at least one color. But then how can I create the node variables itself as the text file can contain any amount of nodes. I know what I have to do for the bunch of if and else statements but I do not get how can I use that to reduce it to a miniSAT format like:
p cnf 3 3
1 -2 0
2 -3 0
-1 3 0
where the first "3" represents the number of variables and the second 3 represents the number of clauses. The other lines consist of different variables such that each different number is its own variable and if it's positive then it is true else false
You're essentially asking for how to reduce 3-coloring to CNF-SAT. Let's start with how to do that, then talk about how to generate the file you need.
The basic idea behind the reduction is the following: for each node x, we'll create three propositional variables: xr, xg, and xb that indicate what color is assigned to the node (red, green, or blue). From there, we need to add in clauses to enforce the following constraints:
every node is given at least one color,
every node is given at most one color, and
no two adjacent nodes are given the same color.
To encode point (1), we can use this clause:
(xr ∨ xg ∨ xb)
To encode point (2), we can use three clauses, each of which rules out the possibility that two colors are assigned:
(¬xr ∨ ¬xg) ∧
(¬xg ∨ ¬xb) ∧
(¬xb ∨ ¬xr)
Finally, there's rule (3). We can basically adapt the above strategy to solve this problem. For each pair of adjacent nodes x and y, add in these clauses:
(¬xr ∨ ¬yr) ∧
(¬xg ∨ ¬yg) ∧
(¬xb ∨ ¬yb)
The overall formula is then made by AND-ing all of these clauses together.
Now, the question is how to generate the output file that you want to generate. There are plenty of ways you could do this. Here's a few options:
You could calculate exactly how many clauses and variables are needed directly from the number of nodes and edges in the graph, then use some clever arithmetic tricks to output each clause by doing some math to figure out which variables correspond to which indices.
You could build some internal representation of variables and clauses (for example, have a map from nodes to the number associated with its color variables, then have a list of clauses), build that representation, then iterate over it to generate the output file.
I'd personally recommend option (2), as I imagine that's probably the easiest way of doing this.
Hope this helps!

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?

How to find polygons in a given set of points and edges?

Consider the following problem:
Given N points in plane and M line segments connecting them, find all polygons (convex or concave) that do not contain any other polygons inside.
For instance:
There are 5 polygons founded:
1 - 2 - 5 - 6
2 - 3 - 5
3 - 4 - 5
7 - 8 - 9
10 - 13 - 20 - 12 - 11
How can I identify these polygons and there corresponding vertices and edges? And what is the fastest solution for this?
Build graph with segment ends as vertices and segments as edges, then find cycles using DFS.
Note that the same edges might belong to multiple cycles (polygons) like your 2-5 and there might be many variants to select cycles. To exclude ambiguity, you can build fundamental set of cycles
Edit. As weston noticed in comments, resulted polygons might contains others. So sketch of more geometric approach:
Build lists of adjacency for graph.
Sort edges in ever list by polar angle.
Choose the most bottom vertex A.
If it's degree is 0, remove vertex, if 1, remove vertex and edge.
Otherwise get edge E with the smallest angle from this vertex.
Walk to pair vertex B.
Choose the most left edge F from B.
Move along to F edge into C.
If dead end, remove F and vertex C and return to B.
Repeat moving using left-hand rule until meet vertex A or dead end vertex.
In the walk process remove edges outgoing from vertices of degree 2 or mark them as used.

Query ArangoDB general-graph for common neighbors with more than 2 start vertices?

Consider the following example graph:
 
Given the vertices A, B and C (creators), how to figure out their common neighbors?(projects all 3 participated in)For two vertices, I could simply use GRAPH_COMMON_NEIGHBORS("myGraph", A, B), but what if I want to query for 3 or more? Expected result: 1 and 2.
Given the same vertices, how can I make it return common neighbors with no other connections?(creators exclusively participated in a project, no additional edges allowed)?Expected result: 1, because 2 has an edge coming from D, which isn't one of the starting vertices.
You can simply pass the same set of vertices as both parameters for common neighbors. Then repack the result in a better format for AQL to compute the intersection:
let res = (
let nodes = ["a/A","a/B","a/C"]
for n in GRAPH_COMMON_NEIGHBORS("g",nodes , nodes)
for f in VALUES(n)
return VALUES(f)
)
return CALL("intersection", res[0])

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.

Resources