Beispiel #1
0
 public MyList()
 {
     headNode = null;
 }
Beispiel #2
0
 public MyNode(int i)
 {
     data = i;
     next = null;
 }
Beispiel #3
0
        static bool listIsBiPartitite(MyList[] array, int n)
        {
            int i = 0;

            // This is an array to define the set of each node:
            // D means Domain
            // R means Range
            // N means Not defined
            char[] sets = new char[n];
            //initialize the nodes with 'N' Not definied
            for (int m = 1; m < n; m++)
            {
                sets[m] = 'N';
            }
            sets[0] = 'D'; //Suppose that the first node is in the domain set
            while (i < n)
            {
                MyNode tmp        = array[i].headNode;          //tmp node to scan the set
                int    headNumber = array[i].headNode.data - 1; //Getting the number of the first node of the list
                if (tmp != null)
                {
                    tmp = tmp.next;
                    while (tmp != null)
                    {
                        if (sets[headNumber] == 'N') //If the  headnode is not in any set we check the set of the other nodes that have connections with it
                        {
                            while (tmp != null)
                            {
                                if (sets[tmp.data - 1] != 'N')
                                //If one of the connections is already in a set then assign the headnode to the opposite set
                                {
                                    if (sets[tmp.data - 1] == 'D')
                                    {
                                        sets[headNumber] = 'R';
                                    }
                                    else
                                    {
                                        sets[headNumber] = 'D';
                                    }
                                    tmp = array[i].headNode; //Go back to the beginning of the list to check the previous unassigned nodes
                                    break;
                                }
                                tmp = tmp.next;
                            }
                            if (tmp == null) //If none of the nodes is assigned then assign the headnode to an arbitary set
                            {
                                sets[headNumber] = 'D';
                                tmp = array[i].headNode;
                            }
                        }
                        else if (sets[headNumber] == sets[tmp.data - 1]) // If two nodes that are connected with each other belong to the same set
                        {
                            return(false);                               //Then the graph is not Bipartite
                        }
                        //Else assign all the nodes connected to the headnode to the opposite set of the headnode
                        else if (sets[headNumber] == 'D')
                        {
                            sets[tmp.data - 1] = 'R';
                        }
                        else
                        {
                            sets[tmp.data - 1] = 'D';
                        }
                        tmp = tmp.next;
                    }
                }
                i++;
            }
            return(true);
        }