public int[] getShortestPath(UndirectedGraph g, int vertex)
        {
            Queue<int> q = new Queue<int>();
            q.Enqueue(vertex);

            //set up the entire distance matrix with -1 except for the vertex node
            for (int i = 0; i < g.getTotalVertices(); i++)
            {
                distanceMatrix[i] = -1;
            }
            //set up current vertex with 0 because there will be no distance
            distanceMatrix[vertex] = 0;

            while (q.Count > 0)
            {
                int v = q.Dequeue();
                Console.Write(v); //this is the breadth first traversal
                ArrayList adjacent = g.Adjacent(v);
                //loop through all the adjacent nodes, if the currentVertex is still -1, then go in and do process
                for (int i = 0; i < adjacent.Count; i++)
                {
                    int currentVertex = (int)adjacent[i];
                    if (distanceMatrix[currentVertex] == -1)
                    {
                        //add one to the distanceMatrix here because it's going to be from v to the current path
                        distanceMatrix[currentVertex] = distanceMatrix[v] + 1;
                        pathMatrix[currentVertex] = v;
                        q.Enqueue(currentVertex);

                    }
                }
            }
            return distanceMatrix;
        }
 public UndirectedGraphBreadthFirst(UndirectedGraph g)
 {
     marked = new bool[g.getTotalVertices()];
     distanceMatrix = new int[g.getTotalVertices()];
     pathMatrix = new int[g.getTotalVertices()];
     visitedMatrix = new State[g.getTotalVertices()];
 }
        public static void main()
        {
            UndirectedGraph g = new UndirectedGraph(13);
            g.AddEdge(0, 6);
            g.AddEdge(0, 2);
            g.AddEdge(0, 1);
            g.AddEdge(0, 5);
            g.AddEdge(1, 0);
            g.AddEdge(2, 0);
            g.AddEdge(3, 5);
            g.AddEdge(3, 4);
            g.AddEdge(4, 5);
            g.AddEdge(4, 6);
            g.AddEdge(4, 3);
            g.AddEdge(5, 3);
            g.AddEdge(5, 4);
            g.AddEdge(5, 0);
            g.AddEdge(6, 0);
            g.AddEdge(6, 4);
            g.AddEdge(7, 8);
            g.AddEdge(8, 7);
            g.AddEdge(9, 11);
            g.AddEdge(9, 10);
            g.AddEdge(9, 12);
            g.AddEdge(10, 9);
            g.AddEdge(11, 9);
            g.AddEdge(11, 12);
            g.AddEdge(12, 11);
            g.AddEdge(12, 9);

            UndirectedGraphDepthFirst depthFirst = new UndirectedGraphDepthFirst(g);
            depthFirst.search(g, 8);

            Queue<int> q = depthFirst.getPath();
            Console.WriteLine("Path is: ");
            foreach (int value in q)
            {
                Console.Write(value + " ");
            }


            for(int i = 0; i < g.getTotalEdges(); i++)
            {
                if (depthFirst.pathExists(i) != false)
                {
                    Console.Write(i);
                }
            }

            Console.WriteLine("Connected: " + depthFirst.isConnected(g));
        }
        public void dfs(UndirectedGraph g, int vertex){
            marked[vertex] = true;
            id[vertex] = count;
            size[vertex]++;

            ArrayList adjacent = g.Adjacent(vertex);
            for (int i = 0; i < adjacent.Count; i++)
            {
                int currentVertex = (int) adjacent[i];
                if(!marked[currentVertex]){
                    dfs(g, currentVertex);
                }
            }
        }
        private int[] size; //number of vertices in a connected component

        public ConnectedComponentGraph(UndirectedGraph g)
        {
            marked = new bool[g.getTotalVertices()];
            id = new int[g.getTotalVertices()];
            size = new int[g.getTotalVertices()];

            for (int i = 0; i < g.getTotalVertices(); i++)
            {
                if (!marked[i])
                {
                    dfs(g, i);
                    count++;
                }
            }
        }
        public static void main()
        {
            UndirectedGraph g = new UndirectedGraph(13);
            g.AddEdge(0, 5);
            g.AddEdge(4, 5);
            g.AddEdge(0, 1);
            g.AddEdge(9, 12);
            g.AddEdge(6, 4);
            g.AddEdge(5, 4);
            g.AddEdge(0, 2);
            g.AddEdge(11, 12);
            g.AddEdge(9, 10);
            g.AddEdge(0, 6);
            g.AddEdge(7, 8);
            g.AddEdge(9, 11);
            g.AddEdge(5, 3);

            ConnectedComponentGraph cc = new ConnectedComponentGraph(g);
            Console.WriteLine("Total connected components: " + cc.numberOfConnectedComponent());

            //display vertices in each connnected component
            Dictionary<int, Queue<int>> dict = new Dictionary<int, Queue<int>>();
            for (int i = 0; i < cc.numberOfConnectedComponent(); i++)
            {
                dict[i] = new Queue<int>();
            }

            for (int i = 0; i < g.getTotalVertices(); i++)
            {
                dict[cc.connectedComponentId(i)].Enqueue(i);
            }

            for (int i = 0; i < cc.numberOfConnectedComponent(); i++)
            {
                Console.WriteLine("\n");
                Console.WriteLine("Component: " + i);
                while (dict[i].Count > 0)
                {
                    Console.Write(dict[i].Dequeue() + " ");
                }
            }
        }
        //do a depth first search on a vertex to find out all the vertices it has a path to.  This also prints out all the vertices
        public void search(UndirectedGraph graph, int vertex)
        {
            totalPaths++;
            marked[vertex] = true;
            ArrayList adj = graph.Adjacent(vertex);

            path.Enqueue(vertex);

            //this is how it traverses
            //Console.WriteLine(vertex);
            
            for(int i = 0; i< adj.Count; i++)
            {
                int currentVertex = (int) adj[i];
                if (marked[currentVertex] != true)
                {
                    search(graph, currentVertex);
                    
                }
            }
        }
Esempio n. 8
0
        public SymbolGraph(String filename, Char delimiter)
        {
            //create all as vertices
            symbolTable = new Dictionary<string, int>();
            allLines = System.IO.File.ReadAllLines(@filename);
            int count = 0;
            foreach (string line in allLines)
            {
                String[] items = line.Split(delimiter);
                for(int i = 0; i < items.Length; i++){
                    if(!symbolTable.ContainsKey(items[i])){
                        symbolTable[items[i]] = count;
                        count++;
                    }
                }
            }

            //invert index to get string keys as an array
            
            keys = new String[symbolTable.Count];
            count = 0;
            foreach (KeyValuePair<string, int> pair in symbolTable)
            {
                keys[count] = pair.Key;
                count++;
            }

            //create graph from dictionary
            graph = new UndirectedGraph(symbolTable.Count);

            for (int i = 0; i < allLines.Length; i++)
            {
                String[] items = allLines[i].Split(delimiter);
                for (int j = 1; j < items.Length; j++)
                {
                    graph.AddEdge(symbolTable[items[0]], symbolTable[items[j]]);
                }
            }
        }
Esempio n. 9
0
        static void Main(string[] args)
        {
            UndirectedGraph g = new UndirectedGraph(6);
            g.AddEdge(0, 2);
            g.AddEdge(0, 1);
            g.AddEdge(0, 5);
            g.AddEdge(1, 0);
            g.AddEdge(1, 2);
            g.AddEdge(2, 0);
            g.AddEdge(2, 1);
            g.AddEdge(2, 3);
            g.AddEdge(2, 4);
            g.AddEdge(3, 5);
            g.AddEdge(3, 4);
            g.AddEdge(3, 2);
            g.AddEdge(4, 3);
            g.AddEdge(4, 2);
            g.AddEdge(5, 3);
            g.AddEdge(5, 0);

            UndirectedGraphBreadthFirst br = new UndirectedGraphBreadthFirst(g);
            int[] shortestPath = br.getShortestPath(g,5);
            //br.bfs(g, 5);
        }
        public void bfs(UndirectedGraph g, int vertex)
        {
            Queue<int> q = new Queue<int>();

            //set up the entire visitedMatrix with -1 except for the vertex node
            for (int i = 0; i < g.getTotalVertices(); i++)
            {
                visitedMatrix[i] = State.Unvisited;
            }
            
            //mark root with 1 to sat it's visited
            visitedMatrix[vertex] = State.Visiting;
            q.Enqueue(vertex);

            while (q.Count > 0)
            {
                int v = q.Dequeue();
                Console.WriteLine(v);
                marked[v] = true;

                //get all adjacent
                ArrayList adj = g.Adjacent(v);
                
                for (int i = 0; i < adj.Count; i++)
                {
                    int currentVertex = (int) adj[i];
                    if (visitedMatrix[currentVertex] == State.Unvisited)
                    {
                        q.Enqueue(currentVertex);
                        visitedMatrix[currentVertex] = State.Visiting;
                    }
                }
                visitedMatrix[v] = State.Visited;
                marked[v] = false;
            }
        }
 public static void main()
 {
     UndirectedGraph g = new UndirectedGraph(13);
     g.AddEdge(0, 6);
     g.AddEdge(0, 2);
     g.AddEdge(0, 1);
     g.AddEdge(0, 5);
     g.AddEdge(1, 0);
     g.AddEdge(2, 0);
     g.AddEdge(3, 5);
     g.AddEdge(3, 4);
     g.AddEdge(4, 5);
     g.AddEdge(4, 6);
     g.AddEdge(4, 3);
     g.AddEdge(5, 3);
     g.AddEdge(5, 4);
     g.AddEdge(5, 0);
     g.AddEdge(6, 0);
     g.AddEdge(6, 4);
     g.AddEdge(7, 8);
     g.AddEdge(8, 7);
     g.AddEdge(9, 11);
     g.AddEdge(9, 10);
     g.AddEdge(9, 12);
     g.AddEdge(10, 9);
     g.AddEdge(11, 9);
     g.AddEdge(11, 12);
     g.AddEdge(12, 11);
     g.AddEdge(12, 9);
 }
 public Boolean isConnected(UndirectedGraph g)
 {
     return totalPaths == g.getTotalVertices();
 }
 public UndirectedGraphDepthFirst(UndirectedGraph graph)
 {
     marked = new bool[graph.getTotalVertices()];
     path = new Queue<int>();
     totalPaths = 0;            
 }
        public static void main()
        {
            UndirectedGraph g = new UndirectedGraph(6);
            g.AddEdge(0, 2);
            g.AddEdge(0, 1);
            g.AddEdge(0, 5);
            g.AddEdge(1, 0);
            g.AddEdge(1, 2);
            g.AddEdge(2, 0);
            g.AddEdge(2, 1);
            g.AddEdge(2, 3);
            g.AddEdge(2, 4);
            g.AddEdge(3, 5);
            g.AddEdge(3, 4);
            g.AddEdge(3, 2);
            g.AddEdge(4, 3);
            g.AddEdge(4, 2);
            g.AddEdge(5, 3);
            g.AddEdge(5, 0);

            UndirectedGraphBreadthFirst br = new UndirectedGraphBreadthFirst(g);
            //int[] shortestPath = br.getShortestPath(g, 0);
            br.bfs(g, 5);

        }