Problem Statement:
To find all the triangles in an undirected graph with vertices of different colours. (Red, Yellow and Green).
Assumptions:
There exists a way to partition the graph into two subsets so that |V1|=|V2| or |V1|=|V2|+1 where the following conditions apply: either every vertex of V1 is connected to every vertex of V2 or no Vertex of V1 is connected to a vertex of V2 . This applies recursively to all induced subgraphs of V1 and V2.
Logic:
We can break the graph into two subgraphs recursively and find a triangle formed between one vertex in V1 and other two in V2 or one vertex in V2 and other two in V1.
At each recursive call, we can partition the given graph into V1, V2 which will satisfy the above property (function partition already given). The recursion breaks when the size of either V1, V2 becomes zero or both become equal to 1. This function is called recursively for both V1 and V2. If there is no edge between V1 and V2, we need not consider this partition for our final triangle list; so we return from this call.
Now, for each vertex in V2, we add to a globally declared colour map for the three colour combinations. Using this map, for each vertex in V2,we check the corresponding other colour combination and add this to the triangle list.
Pseudo Implementation
//let g be the given graph.
//Vertex be the class representing each vertex ( will have attributes 'vertex_number' + 'colour')
//let Edge be the class representing edges ( will have attributes 'a' and 'b' corresponding to two edges
//let (v1,v2) = partition(g) be the given function which can partition the graph into V1, v2.
//let adjacency_list be the ArrayList<ArrayList<Vertex>> containing the Adjacency list for the given vertices
//Main Callee Method
HashMap<String, List<Edge>> edge_list = new HashMap<String, List<Edge>>()
ArrayList<ArrayList<Vertex>> adjacency_list = new ArrayList<ArrayList<Vertex>>()
edge_list.put('rg', new ArrayList<Edge>())
edge_list.put('gy', new ArrayList<Edge>())
edge_list.put('yr', new ArrayList<Edge>())
ArrayList<new ArrayList<Vertex>> triangle_list = new ArrayList<new ArrayList<Vertex>>()
getColouredTriangles(g)
//Recursive Implementation of Coloured Triangle method
getColouredTriangles(g):
(v1,v2) = partition(g)
//If size is zero or both have size as 1 no triangles can be formed
if v1.size() == 0 || v2.size() == 0 || (v1.size() == 1 && v2.size() == 1):
return
//Calling recursively for both v1 and v2
getColouredTriangles(v1)
getColouredTriangles(v2)
//If there is no edge between the two subgraphs, return as no triangle is possible now between v1 and v2.
if not edge(v1.get(0), v2.get(0)):
return
//call for one vertex in v1, two in v2
getTrianglesInTwoGraphs(v1,v2)
//call for one vertex in v2, two in v1
getTrianglesInTwoGraphs(v2,v1)
//Method to get triangles between two graphs with one vertex in v1 and other two in v2.
getTrianglesInTwoGraphs(v1,v2):
//Form edge_list having colour to Edge mapping
for v in v2:
for vertex in adjacency_list.get(v):
if vertex in v2:
String colour = v.colour + vertex.colour
if(edge_list.get(colour) == null):
colour = vertex.colour + v.colour
edge_list.colour.put(colour,vertex.edge)
//for each v in v1, check other coloured edges from edge_list
for v in v1:
ArrayList<Edge> edges = new ArrayList<Edge>()
if v.colour == r:
edges = edge_list.get("gy")
else if v.colour == g:
edges = edge_list.get("yr")
else:
edges = edge_list.get("rg")
for edge in edges:
ArrayList<Vertex> vertices = new ArrayList<Vertex>()
vertices.add(v)
vertices.add(edge.a)
vertices.add(edge.b)
triangle_list.add(vertices)
Result:
The global variable triangle_list contains the vertex groups with coloured triangles.