private static Map<Integer, List<Integer>> removeEdgeAndContractGraph(Map<Integer, List<Integer>> adjacencyMap, int vertex1, int vertex2){
        //Pick the smaller vertex of two. Also replace the vertex no in all other vertices.

        int base1 = vertex1;  //contracting into the smaller vertex
        int base2= vertex2;
        if(vertex2 < vertex1) {
            base1= vertex2;
            base2= vertex1;
        } 

        for (int i: adjacencyMap.get(base2)){
            //Check self loops just in case
            if (i== base1 || i==base2) continue;
            adjacencyMap.get(base1).add(i);
            while(adjacencyMap.get(i).contains(base2)){
                adjacencyMap.get(i).remove((Integer)base2);
                adjacencyMap.get(i).add((Integer)base1);
            }
        }

        while (adjacencyMap.get(base1).contains(base2)){
            adjacencyMap.get(base1).remove((Integer)base2);
        }
        
        adjacencyMap.remove(base2);

        return adjacencyMap;
    }