You need to allocate marks - int array of length n, where n is the number of vertex in graph and fill it with zeros. Then: 1) For BFS do the following: Components = 0; Enumerate all vertices, if for vertex number i, marksi == 0 then ++Components; Put this vertex into queue, and while queue is not empty, pop vertex v from q marksv = Components; Put all adjacent vertices with marks equal to zero into queue 2) For DFS do the following Components = 0; Enumerate all vertices, if for vertex number i, marksi == 0 then ++Components; Call DFS(i, Components), where DFS is DFS(vertex, Components) { marksvertex = Components; Enumerate all vertices adjacent to vertex and for all vertex j for which marksj == 0 call DFS(j, Components); } After performing any of this procedures, Components will have number of connected components, and for each vertex i, marksi will represent index of connected component I belongs Both complete on O(n) time, using O(n) memory, where n is matrix size. But I suggest you BFS as far as it doesn't suffer from stack overflow problem, and it doesn't spend time on recursive calls BFS code in Java: public static boolean BFS(boolean adjacencyMatrix, int vertexCount, int givenVertex){ // Result array.
Boolean mark = new booleanvertexCount; Queue queue = new LinkedList(); queue. Add(givenVertex); markgivenVertex = true; while (!queue.isEmpty()) { Integer current = queue.remove(); for (int I = 0; I Add(i); } } return mark; } public static void main(String args) { // Given adjacencyMatrixxy if and only if there is a path between x and y. Boolean adjacencyMatrix = new boolean { {false,true,false,false,false}, {true,false,false,true,true}, {false,false,false,false,false}, {true,false,false,false,false}, {true,false,false,false,false} }; // Marki is true if and only if I belongs to the same connected component as givenVertex vertex does.
Boolean mark = BFS(adjacencyMatrix, 5, 0); for (int I = 0; I Println(marki) }.
You need to allocate marks - int array of length n, where n is the number of vertex in graph and fill it with zeros. Then: 1) For BFS do the following: Components = 0; Enumerate all vertices, if for vertex number i, marksi == 0 then ++Components; Put this vertex into queue, and while queue is not empty, pop vertex v from q marksv = Components; Put all adjacent vertices with marks equal to zero into queue. 2) For DFS do the following.
Components = 0; Enumerate all vertices, if for vertex number i, marksi == 0 then ++Components; Call DFS(i, Components), where DFS is DFS(vertex, Components) { marksvertex = Components; Enumerate all vertices adjacent to vertex and for all vertex j for which marksj == 0 call DFS(j, Components); } After performing any of this procedures, Components will have number of connected components, and for each vertex i, marksi will represent index of connected component I belongs. Both complete on O(n) time, using O(n) memory, where n is matrix size. But I suggest you BFS as far as it doesn't suffer from stack overflow problem, and it doesn't spend time on recursive calls.
BFS code in Java: public static boolean BFS(boolean adjacencyMatrix, int vertexCount, int givenVertex){ // Result array. Boolean mark = new booleanvertexCount; Queue queue = new LinkedList(); queue. Add(givenVertex); markgivenVertex = true; while (!queue.isEmpty()) { Integer current = queue.remove(); for (int I = 0; I Marki) { marki = true; queue.
Add(i); } } return mark; } public static void main(String args) { // Given adjacencyMatrixxy if and only if there is a path between x and y. Boolean adjacencyMatrix = new boolean { {false,true,false,false,false}, {true,false,false,true,true}, {false,false,false,false,false}, {true,false,false,false,false}, {true,false,false,false,false} }; // Marki is true if and only if I belongs to the same connected component as givenVertex vertex does. Boolean mark = BFS(adjacencyMatrix, 5, 0); for (int I = 0; I.
If you need the exact code, I can add it for you. – Wisdom's Wind Nov 14 at 16:42 Thankyou, I've realised my original question wasn't too clear. I need to find the connected component (so other reachable vertices) for a given vertex.
I realise this is probably similar but I can't visualise it, could you give me a similar pseudo code? – Denti Nov 14 at 16:47 1 All you need is to drop top enumeration circle, and start from Components = 1: 1) For BFS you need to put your given vertex into queue and follow the algorithm. 2) For DFS just call DFS(your vertex, 1).
After that for all vertices I belongs to the same connected component as your given vertex you will have marksi == 1, and marksi == 0 for others. – Wisdom's Wind Nov 14 at 16:52 Sorry I don't quite understand what you mean by dropping the top enumeration circle, do you mind writing it again? Thanks.
Also which is best to use for this problem BFS or DFS? – Denti Nov 14 at 16:56 When I said "drop", I meant that is redundant in your case, you don't have to write it. Read the last two lines I've add to the original answer, I suggest you to use BFS.
– Wisdom's Wind Nov 14 at 16:59.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.