C#的数据结构之图

namespace Graph
{
    
public class Graph
    
{
        
int[,] v;
        
int num;
        
public Graph(int n)
        
{
            num 
= n;
            v
=new int[n,n];
            
for (int i = 0; i < n; i++)
            
{
                
for (int j = 0; j < n; j++)
                
{
                    v[i, j] 
= int.MinValue;
                    
if (i==j)
                    
{
                        v[i, j] 
= -1;
                    }

                }

            }

        }

        
public void CreateGraph(int a,int b,int weight)
        
{
            v[a, b] 
= weight;
            v[b, a] 
= weight;
        }

        
public int FirstN(int v1)
        
{
            
for (int i = 0; i < num; i++)
            
{
                
if (v[v1,i]>=0)
                
{
                    
return i;
                    
                }

            }

            
return -1;
        }

        
public int NextN(int v1, int w)
        
{
            
for (int i = w + 1; i < num; i++)
            
{
                
if (v[v1,i]>=0)
                
{
                    
return i;
                }

            }

            
return -1;
        }

        
/// <summary>
        
/// 边数目
        
/// </summary>

        public int EdgeNum
        
{
            
get 
            
{
                
int result = 0;
                
for (int i = 0; i < num; i++)
                
{
                    
for (int j = i+1; j < num; j++)
                    
{
                        
if (v[i, j] >= 0)
                        
{
                            result
++;
                        }

                    }

                    
                }

                
return result;
            }

        }


        
public void DFS()
        
{
            
//define a visit array.
            
//use it to remember the visited Vertux
            int[] visited = new int[num];
            
for (int i = 0; i < num; i++)
            
{
                visited[i] 
= 0;
            }

            DFS(
0, visited);

        }

        
void DFS(int v, int[] visited)
        
{
            Console.Write(
"{0} ", v);
            visited[v] 
= 1;
            
int w = FirstN(v);
            
while (w != -1)
            
{
                
if (visited[w]==0)
                    DFS(w, visited);
                w 
= NextN(v, w);
            }

        }

        
public void BFS()
        
{
            Queue
<int> q = new Queue<int>();
            
int[] visited = new int[num];
            
for (int i = 0; i < num; i++) visited[i] = 0;
            
int v = 0;
            Console.Write(
"{0} ", v);
            visited[v] 
= 1;
            q.Enqueue(v);
            
while (q.Count!=0)
            
{
                v 
= q.Dequeue();
                
int w = FirstN(v);
                
while (w!=-1)
                
{
                    
if (visited[w]==0)
                    
{
                        
                        Console.Write(
"{0} ", w);
                        visited[w] 
= 1;
                        q.Enqueue(w);
                    }

                    w 
= NextN(v, w);
                }

            }

        }

    }
    
}
posted @ 2008-01-12 11:04  zeus2  Views(674)  Comments(0Edit  收藏  举报