3

In an undirected Graph G=(V,E) the vertices are colored either red, yellow or green. Furthermore there exist 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

I can find all triangles in the Graph by multiplying the adjacency matrix with itself three times and step up the nodes corresponding to the non zero entries of the main diagonal. Then I can see if the nodes of the triangle are colored the right way. O(n^~2,8)! But given the unique properties of the graph I want to find a solution using divide and conquer to find the colored triangle. this is an example graph with the given properties. I need to find the bold triangle: this is an example graph with the given properties. I need to find the bold triangle Blue boxes symbolize the partitions are fully connected, purple boxes mean no connection between the partitions

Community
  • 1
  • 1
Gilfoyle
  • 301
  • 1
  • 10
  • What is the question exactly? – RobertBaron Jun 06 '19 at 23:58
  • to find a 3 colored triangle in with a divide and conquer approach in an undirected graph. I added a picture for better understanding – Gilfoyle Jun 07 '19 at 20:28
  • Yes, I saw it. Thanks! – RobertBaron Jun 07 '19 at 20:31
  • Is there any connection between the coloring of nodes and the partitions? Also, can the partitions of the original graph and subgraphs be easily obtained or not? If the partitions need to be computed on the fly with considerable overhead it would be difficult to develop an efficient algorithm that uses this property. – GZ0 Jun 07 '19 at 21:05
  • There is no connection between the coloring of nodes and the partitions, also the partitions have to be computed on the fly – Gilfoyle Jun 09 '19 at 07:17

2 Answers2

5

It can be done in O(E*V) without using the partition property. Start by deleting all edges with the same color on both vertexes, this can be done in O(E). In the modified graph G', every triangle is a 3-colored triangle. Finding the triangles in a graph:

for each edge e(u,v):
    for each vertex w:
        if e(v,w) and e(u,w) in G':
            add (u,v,w) to triangle list

If you keep adjacency list as well as adjacency matrix, you can improve the time of the inner loop by checking only w's in the adjacency list of v. In that case the complexity is O(E * max(deg(v)).

Shuki Avraham
  • 1,033
  • 1
  • 7
  • 14
1

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.

Jahnavi Paliwal
  • 1,721
  • 1
  • 12
  • 20
  • Good work. I'm not sure how the `partition` function can be implemented efficiently to make the whole algorithm at least as efficient as in the previous answer. – GZ0 Jun 12 '19 at 14:01
  • 1
    Yes. The implementation for the partition function will have to be thought through. The previous answer algorithm works better in general with the complexity `0(V*E)`. But as the constraint is to use `divide and conquer` .I am using the given `partition` function to break into subproblems. – Jahnavi Paliwal Jun 13 '19 at 06:51
  • The problem statement only says there exists a partition but does not explicitly say that there is a `partition` function provided. I was on the assumption that this function also needs to be provided in the solution and I could not figure out how that could be done in an efficent way. – GZ0 Jun 14 '19 at 01:38