11package DataStructures .Graphs ;
22
3+ import java .util .List ;
4+ import java .util .Queue ;
5+ import java .util .ArrayList ;
6+ import java .util .LinkedList ;
7+
38public class MatrixGraphs {
49
510 public static void main (String args []) {
@@ -12,7 +17,17 @@ public static void main(String args[]) {
1217 graph .addEdge (3 , 4 );
1318 graph .addEdge (4 , 1 );
1419 graph .addEdge (2 , 3 );
20+ graph .addEdge (3 , 9 );
21+ graph .addEdge (9 , 1 );
22+ graph .addEdge (9 , 8 );
23+ graph .addEdge (1 , 8 );
24+ graph .addEdge (5 , 6 );
25+ System .out .println ("The graph matrix:" );
1526 System .out .println (graph );
27+ System .out .println ("Depth first order beginning at node '1':" );
28+ System .out .println (graph .depthFirstOrder (1 ));
29+ System .out .println ("Breadth first order beginning at node '1':" );
30+ System .out .println (graph .breadthFirstOrder (1 ));
1631 }
1732}
1833
@@ -118,6 +133,107 @@ public boolean removeEdge(int from, int to) {
118133 return false ;
119134 }
120135
136+ /**
137+ * This method returns a list of the vertices in a depth first order
138+ * beginning with the specified vertex
139+ *
140+ * @param startVertex the vertex to begin the traversal
141+ * @return the list of the ordered vertices
142+ */
143+ public List <Integer > depthFirstOrder (int startVertex ) {
144+ // If the startVertex is invalid, return an empty list
145+ if (startVertex >= _numberOfVertices || startVertex < 0 )
146+ return new ArrayList <Integer >();
147+
148+ // Create an array to track the visited vertices
149+ boolean [] visited = new boolean [_numberOfVertices ];
150+
151+ // Create a list to keep track of the order of our traversal
152+ ArrayList <Integer > orderList = new ArrayList <Integer >();
153+
154+ // Perform our DFS algorithm
155+ depthFirstOrder (startVertex , visited , orderList );
156+
157+ return orderList ;
158+ }
159+
160+ /**
161+ * Helper method for public depthFirstOrder(int) that will perform
162+ * a depth first traversal recursively on the graph
163+ *
164+ * @param currentVertex the currently exploring vertex
165+ * @param visited the array of values denoting whether or not that vertex has been visited
166+ * @param orderList the list to add vertices to as they are visited
167+ */
168+ private void depthFirstOrder (int currentVertex , boolean [] visited , List <Integer > orderList ) {
169+ // If this vertex has already been visited, do nothing and return
170+ if (visited [currentVertex ])
171+ return ;
172+
173+ // Visit the currentVertex by marking it as visited and adding it
174+ // to the orderList
175+ visited [currentVertex ] = true ;
176+ orderList .add (currentVertex );
177+
178+ // Get the adjacency array for this vertex
179+ int [] adjacent = _adjacency [currentVertex ];
180+ for (int i = 0 ; i < adjacent .length ; i ++)
181+ // If an edge exists between the currentVertex and the vertex
182+ // we are considering exploring, recurse on it
183+ if (adjacent [i ] == AdjacencyMatrixGraph .EDGE_EXIST )
184+ depthFirstOrder (i , visited , orderList );
185+ }
186+
187+ /**
188+ * This method returns a list of the vertices in a breadth first order
189+ * beginning with the specified vertex
190+ *
191+ * @param startVertex the vertext to begin the traversal
192+ * @return the list of the ordered vertices
193+ */
194+ public List <Integer > breadthFirstOrder (int startVertex ) {
195+ // If the specified startVertex is invalid, return an empty list
196+ if (startVertex >= _numberOfVertices || startVertex < 0 )
197+ return new ArrayList <Integer >();
198+
199+ // Create an array to keep track of the visited vertices
200+ boolean [] visited = new boolean [_numberOfVertices ];
201+
202+ // Create a list to keep track of the ordered vertices
203+ ArrayList <Integer > orderList = new ArrayList <Integer >();
204+
205+ // Create a queue for our BFS algorithm and add the startVertex
206+ // to the queue
207+ Queue <Integer > queue = new LinkedList <Integer >();
208+ queue .add (startVertex );
209+
210+ // Continue until the queue is empty
211+ while (! queue .isEmpty ()) {
212+ // Remove the first vertex in the queue
213+ int currentVertex = queue .poll ();
214+
215+ // If we've visited this vertex, skip it
216+ if (visited [currentVertex ])
217+ continue ;
218+
219+ // We now visit this vertex by adding it to the orderList and
220+ // marking it as visited
221+ orderList .add (currentVertex );
222+ visited [currentVertex ] = true ;
223+
224+ // Get the adjacency array for the currentVertex and
225+ // check each node
226+ int [] adjacent = _adjacency [currentVertex ];
227+ for (int vertex = 0 ; vertex < adjacent .length ; vertex ++)
228+ // If an edge exists between the current vertex and the
229+ // vertex we are considering exploring, we add it to the queue
230+ if (adjacent [vertex ] == AdjacencyMatrixGraph .EDGE_EXIST )
231+ queue .add (vertex );
232+ }
233+
234+ return orderList ;
235+ }
236+
121237 /**
122238 * this gives a list of vertices in the graph and their adjacencies
123239 *
0 commit comments