public ArrayList depthFirstTraversal(int[][] adjacency_matrix, int source) { //A matrix and a source index is passed in //The traversal will be performed on this matrix and begin from the source index //Stack is created //This stack will be used to process nodes in LIFO order when an unvisited node is found Stack stack = new Stack<>(); int numNodes = adjacency_matrix.length; //boolean array is created //This array will have a slot for each node //Whenever an unvisited node is visited it's slot in this array will be marked boolean[] visited = new boolean[numNodes]; //This variable will hold the index of the element currently being analyzed int element; //The result of the traversal will be stored in this arraylist ArrayList traversal = new ArrayList(); //Since the source index is passed in it has already been visited visited[source] = true; //The source index is added to the queue to continue processing it stack.push(source); while (!stack.isEmpty()) { //The first item from the stack is popped off and stored in element //___________ //Since this item has been processed it is added to the resulting array list //___________ for (int i = 0; i < numNodes; i++) { //At the row in the matrix where element is stored //If that slot is not zero it means that node can be traveled to from the current element //If that node has not been checked off in the visited array if (adjacency_matrix[element][i] != 0 && visited[i] == false) { //The index currently being looked at will be pushed on to the stack //___________ //That index has now been visited, so it is checked off in the visited array //___________ } } } //Once the traversal has been completed the array list is returned return traversal; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

public ArrayList<Integer> depthFirstTraversal(int[][] adjacency_matrix, int source)
{
//A matrix and a source index is passed in
//The traversal will be performed on this matrix and begin from the source index

//Stack is created
//This stack will be used to process nodes in LIFO order when an unvisited node is found
Stack<Integer> stack = new Stack<>();
int numNodes = adjacency_matrix.length;

//boolean array is created
//This array will have a slot for each node
//Whenever an unvisited node is visited it's slot in this array will be marked
boolean[] visited = new boolean[numNodes];

//This variable will hold the index of the element currently being analyzed
int element;

//The result of the traversal will be stored in this arraylist
ArrayList<Integer> traversal = new ArrayList<Integer>();

//Since the source index is passed in it has already been visited
visited[source] = true;

//The source index is added to the queue to continue processing it
stack.push(source);

while (!stack.isEmpty()) {

//The first item from the stack is popped off and stored in element
//___________
//Since this item has been processed it is added to the resulting array list
//___________

for (int i = 0; i < numNodes; i++) {
//At the row in the matrix where element is stored
//If that slot is not zero it means that node can be traveled to from the current element
//If that node has not been checked off in the visited array
if (adjacency_matrix[element][i] != 0 && visited[i] == false) {
//The index currently being looked at will be pushed on to the stack
//___________

//That index has now been visited, so it is checked off in the visited array
//___________
}
}
}
//Once the traversal has been completed the array list is returned
return traversal;
}

X443: Graphs - Depth First Traversal
Given is an adjacency matrix and a starting index.
Complete the depth first traversal method. Comments noted with //
are lines that must be completed
java.util.Stack and java.util.ArrayList have been enabled.
Examples:
depthFirstTraversal({{0, 1, 0, 1}, {0, 0, 1, 0}, (0, 0, 0, 1}, {0, 1, 8, 0}},0) -> new java.util.ArrayList<Integer>(){{add(0); add(3); add (1); add(2);}}
Your Answer:
Feedback
public ArrayList<Integer> depthFirstTraversal (int00 adjacency_matrix, int source)
Your feedback will appear here when you check your answer.
//A matríx and a source index is passed in
//The traversal will be performed on this matrix and begin from the source index
//stack is created
//This stack will be used to process nodes in LIFO order when an unvisited node is found
Stack<Integer> stack - new Stacko)
int numNodes - adjacency_matrix.length;
9
10
11
//boolean array is created
//This array will have a slot for each node
//Whenever an unvisited node is visited it's slot in this array will be marked
boolean[] visited - new boolean[numNodes);
12
13
14
15
16
//This variable will hold the index of the element currently being analyzed
17
int element;
18
19
//The result of the traversal will be stored in this arraylist
ArrayList<Integer> traversal - new ArrayList<Integer> ();
20
21
22
//since the source index is passed in it has already been visited
23
visited[source] - true;
24
25
//The source index is added to the queue to continue processing it
stack.push (source);
26
27
28
while (Istack.isEmpty() {
29
30
//The first item from the stack is popped off and stored ín element
Transcribed Image Text:X443: Graphs - Depth First Traversal Given is an adjacency matrix and a starting index. Complete the depth first traversal method. Comments noted with // are lines that must be completed java.util.Stack and java.util.ArrayList have been enabled. Examples: depthFirstTraversal({{0, 1, 0, 1}, {0, 0, 1, 0}, (0, 0, 0, 1}, {0, 1, 8, 0}},0) -> new java.util.ArrayList<Integer>(){{add(0); add(3); add (1); add(2);}} Your Answer: Feedback public ArrayList<Integer> depthFirstTraversal (int00 adjacency_matrix, int source) Your feedback will appear here when you check your answer. //A matríx and a source index is passed in //The traversal will be performed on this matrix and begin from the source index //stack is created //This stack will be used to process nodes in LIFO order when an unvisited node is found Stack<Integer> stack - new Stacko) int numNodes - adjacency_matrix.length; 9 10 11 //boolean array is created //This array will have a slot for each node //Whenever an unvisited node is visited it's slot in this array will be marked boolean[] visited - new boolean[numNodes); 12 13 14 15 16 //This variable will hold the index of the element currently being analyzed 17 int element; 18 19 //The result of the traversal will be stored in this arraylist ArrayList<Integer> traversal - new ArrayList<Integer> (); 20 21 22 //since the source index is passed in it has already been visited 23 visited[source] - true; 24 25 //The source index is added to the queue to continue processing it stack.push (source); 26 27 28 while (Istack.isEmpty() { 29 30 //The first item from the stack is popped off and stored ín element
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education