Ejemplo n.º 1
0
		public Edge(Vertex n1, Vertex n2) {
			v1=n1;
			v2=n2;
			
			int seed = n1.Name+n2.Name;
			Random rand = new Random(seed);
			Weight = rand.Next(0,50);
		}
Ejemplo n.º 2
0
	public void BuildGraph() {
		
		// Build Nodes	
		Console.WriteLine("Building Vertices...");
		for(int i=0;i< Nodes; i++) {
			Vertex temp = new Vertex(i);
			if(Vfirst==null) {
			     Vfirst = temp;
			     Vlast = temp;
		        }
		        else {
			     temp.AddVertex(Vlast,temp);
			     Vlast = temp;
                        }
			Console.WriteLine("Vertex {0} built...",i);
		}
		
		// Build Edges
		Console.WriteLine("Building Edges...");
	
		DateTime time = DateTime.Now;
		Int32 seed = (Int32)time.Ticks;
		Random rand = new Random(seed);
		
		for(int i=0;i< Nodes;i++) {
		    
		    int j = rand.Next(0,Nodes);
		    for(int k=0;k<j;k++) {
		       int v2;
		       while((v2 = rand.Next(0,Nodes))==i);     //select a random node, also avoid self-loops
		       BuildEdge(i,v2);                //build edge betn node i and v2
		       //Console.WriteLine("Edge built between {0} and {1}...",i,v2);
	              
		       
		    }		
		}
	}
Ejemplo n.º 3
0
		public Vertex(int val) {
			Name = val;
			Next = null;
			Adjacent = new Vertex[Graph.Nodes];	
		}
Ejemplo n.º 4
0
		public void AddVertex(Vertex x, Vertex y) {
			x.Next = y;				
		}
Ejemplo n.º 5
0
	/*public void TraverseGraph() {
		Vertex root = Vfirst;
		int i=0,j=0;
		Vertex next = root.Adjacent[i];
		
		while(j<Nodes) {
			
				Console.WriteLine("root: " + root.Name);
				while(next != null) {
					Console.WriteLine(next.Name);
					if(next.Name == j) {break;}
					next = next.Adjacent[0]; 
				}
				i++;
				if((next = root.Adjacent[i]) == null) {
					i=0;
					j++;
					if(root == Vlast) break;
					else root = root.Next;
					next = root.Adjacent[i];
					
		                }
	        }	
		
	}*/
	
	public void DeleteVertex() {
		Vertex temp1 = null;
		Vertex temp2 = Vfirst;

		DateTime time = DateTime.Now;
		Int32 seed = (Int32)time.Ticks;
		Random rand = new Random(seed);
		
		int j = rand.Next(0,Nodes);
		//Console.WriteLine("Deleting vertex: " + j);

		while(temp2 != null) {
			int i = Decimal.Compare(j,temp2.Name);
			if(i==0) {
				if(temp2 == Vfirst) {
					temp2 = null;
					Vfirst = Vfirst.Next;
					break;
				}
				temp1.Next = temp2.Next;
				temp2 = null;
				break;
				
			}
			else {
				temp1 = temp2;
				temp2 = temp2.Next;
			}
		}
		
		// Restructuring the Graph
		Console.WriteLine("Restructuring the Graph...");
		temp2=Vfirst;
		while(temp2 != null) {
			temp2.DeleteAdjacentEntry(j);
			temp2 = temp2.Next;
		}

		Edge e1 = null;
		Edge e2 = Efirst;
		Edge temp = null;

		while(e2 != null) {
			int v1 = Decimal.Compare(j,e2.v1.Name);
			int v2 = Decimal.Compare(j,e2.v2.Name);
			if((v1==0) || (v2==0)) {
				if(e2 == Efirst) {
					temp = e2;
					e2 = e2.Next;
					Efirst = Efirst.Next;
					temp = null;
					
				}
				else {
					temp = e1;
					e1.Next = e2.Next;
					e2 = e2.Next;
				}
			}
			else {
				e1 = e2;
				e2 = e2.Next;
			}	
		}
	}
Ejemplo n.º 6
0
        public void BuildEdge(int v1, int v2)
        {
            Vertex n1 = null, n2 = null;
            Vertex temp = _vfirst;

            while (temp != null)
            {
                int i = Decimal.Compare(v1, temp.Name);
                if (i == 0)
                {
                    //found 1st node..
                    n1 = temp;
                    break;
                }
                else
                {
                    temp = temp.Next;
                }
            }

            //check if edge already exists
            for (int i = 0; i < n1.Num_Edges; i++)
            {
                int j = Decimal.Compare(v2, n1.Adjacent[i].Name);
                if (j == 0)
                {
                    return;
                }
            }

            temp = _vfirst;
            while (temp != null)
            {
                int i = Decimal.Compare(v2, temp.Name);
                if (i == 0)
                {
                    //found 2nd node..
                    n2 = temp;
                    break;
                }
                else
                {
                    temp = temp.Next;
                }
            }

            n1.Adjacent[n1.Num_Edges++] = n2;

            Edge temp2 = new Edge(n1, n2);

            if (_efirst == null)
            {
                _efirst = temp2;
                _elast  = temp2;
            }
            else
            {
                temp2.AddEdge(_elast, temp2);
                _elast = temp2;
            }
        }
Ejemplo n.º 7
0
 public void AddVertex(Vertex x, Vertex y)
 {
     x.Next = y;
 }
Ejemplo n.º 8
0
 public Vertex(int val)
 {
     Name     = val;
     Next     = null;
     Adjacent = new Vertex[Graph.Nodes];
 }
Ejemplo n.º 9
0
        /*public void TraverseGraph() {
         *  Vertex root = Vfirst;
         *  int i=0,j=0;
         *  Vertex next = root.Adjacent[i];
         *
         *  while(j<Nodes) {
         *
         *          TestLibrary.Logging.WriteLine("root: " + root.Name);
         *          while(next != null) {
         *              TestLibrary.Logging.WriteLine(next.Name);
         *              if(next.Name == j) {break;}
         *              next = next.Adjacent[0];
         *          }
         *          i++;
         *          if((next = root.Adjacent[i]) == null) {
         *              i=0;
         *              j++;
         *              if(root == Vlast) break;
         *              else root = root.Next;
         *              next = root.Adjacent[i];
         *
         *                  }
         *      }
         *
         * }*/

        public void DeleteVertex()
        {
            Vertex temp1 = null;
            Vertex temp2 = _vfirst;

            DateTime time = DateTime.Now;
            Int32    seed = (Int32)time.Ticks;
            Random   rand = new Random(seed);

            int j = rand.Next(0, Nodes);

            //TestLibrary.Logging.WriteLine("Deleting vertex: " + j);

            while (temp2 != null)
            {
                int i = Decimal.Compare(j, temp2.Name);
                if (i == 0)
                {
                    if (temp2 == _vfirst)
                    {
                        temp2   = null;
                        _vfirst = _vfirst.Next;
                        break;
                    }
                    temp1.Next = temp2.Next;
                    temp2      = null;
                    break;
                }
                else
                {
                    temp1 = temp2;
                    temp2 = temp2.Next;
                }
            }

            // Restructuring the Graph
            TestLibrary.Logging.WriteLine("Restructuring the Graph...");
            temp2 = _vfirst;
            while (temp2 != null)
            {
                temp2.DeleteAdjacentEntry(j);
                temp2 = temp2.Next;
            }

            Edge e1   = null;
            Edge e2   = _efirst;
            Edge temp = null;

            while (e2 != null)
            {
                int v1 = Decimal.Compare(j, e2.v1.Name);
                int v2 = Decimal.Compare(j, e2.v2.Name);
                if ((v1 == 0) || (v2 == 0))
                {
                    if (e2 == _efirst)
                    {
                        temp    = e2;
                        e2      = e2.Next;
                        _efirst = _efirst.Next;
                        temp    = null;
                    }
                    else
                    {
                        temp    = e1;
                        e1.Next = e2.Next;
                        e2      = e2.Next;
                    }
                }
                else
                {
                    e1 = e2;
                    e2 = e2.Next;
                }
            }
        }