Write a function which performs Breadth First Search on the given graph. Also write in which order the nodes are visited.
Class Node {
public int value;
public Node[] neighbors;
public boolean visited;
public Node(int num) {
neighbors = new Node[5];
visited = False;
data = num
}
}
-------------------------------------------------------------------------------------------------------------
// Node.java File
public class Node {
public int value;
public Node[] neighbors;
public int neighborSize;
public boolean visited;
public Node(int num) {
neighbors = new Node[5];
visited = false;
value = num;
neighborSize = 0;
}
// self added function
// The below function attaches a particular node as a
neighbor
public void addNeighbor(Node x) {
neighbors[neighborSize++] =
x;
}
}
// BFS.java File
import java.util.LinkedList;
import java.util.Queue;
import java.util.HashMap;
public class BFS {
public static void main(String[] args) {
// make 7 Nodes corresponding to
the figure in the question
Node nA = new Node(40);
Node nB = new Node(10);
Node nC = new Node(20);
Node nD = new Node(30);
Node nE = new Node(50);
Node nF = new Node(60);
Node nG = new Node(70);
//add neighbors for nA
nA.addNeighbor(nB);
nA.addNeighbor(nC);
//add neighbors for nB
nB.addNeighbor(nD);
//add neighbors for nC
nC.addNeighbor(nB);
nC.addNeighbor(nD);
nC.addNeighbor(nE);
nC.addNeighbor(nF);
//add neighbors for nD
nD.addNeighbor(nF);
//add neighbors for nE
nE.addNeighbor(nG);
//add neighbors for nF
nF.addNeighbor(nF);
Queue<Node> q = new
LinkedList<>();
//The below Hashmap is for checking
a vertex with a particular Integer value has been visited or
not
HashMap<Integer,Boolean> map
= new HashMap<>();
q.add(nA);
map.put(nA.value, true);
while(!q.isEmpty()) {
Node top =
q.poll();
System.out.println("Currently at node whose value is
"+top.value);
//check the
neighbors of top
for(int
i=0;i<top.neighborSize;i++) {
if(!map.containsKey(top.neighbors[i].value))
{
q.add(top.neighbors[i]);
map.put(top.neighbors[i].value, true);
}
}
}
}
}
-------------------------------------------------------------------------------------------------------------
// ScreenShot of Output
-------------------------------------------------------------------------------------------------------------
Write a function which performs Breadth First Search on the given graph. Also write in which order the nodes are visited. Class Node { public int value; public Node[] neighbors; public boolean visite...
Use what you learned in recitation to write the nodes visited after each function: node* searchPreorder(int value, node* current) node* searchPostorder(int value, node*current) node* searchlnorder(int value, node* current) on the root of following binary search tree Show to your TA the visited nodes in the form of arrays, i. e. write down the arrays: int inOrderArray[6]; int postOrderArray[6]; int preOrderArray[6];
public class Node { public int data; public Node next; public Node(int d, Node n) { data = d; next = n; } } Please complete the following 5 tasks. 1./** * * @param start * @param low * @param high * @return the sum of all values starting at start that are * in the range [low...high] (inclusive on both sides). 0 if none. * start -> 10 -> 70 -> 20 -> 80 -> null * low =...
PROMPT:
Consider a graph G. A connected component is a maximal subset of
nodes that induces a connected sub graph. It’s maximal in the sense
that you cannot add a node with the resulting induced sub graph
remaining connected.The following function
numComponents returns the number of connected
components in an undirected graph.
QUESTION:
What is the time complexity for this function? The time
complexity should be a function of the number of nodes |V| and the
number of edges |E|....
discrete 2
question 31
For Esercises 25.28, write the nodes in a breadth first search of the graph for Exercises 21 the node specified 25、 26, g 20. In the computer network in the accompanying figure, the same message is to be broade Dribe ( 21-24 28. e 27. to nodes 4.Е. F and G. One way to do this is to find the shortest path from C to send out multiple copies of the same message. A more etficient...
public static boolean primeCheck (int num) int temp; boolean isPrime-true: for(int i-2;i<=num/ 2 ; i++) temp-numsi if (temp=-0) isPrime-false; break; return isPrime Given the above code segment, 1. (30 points) Draw a Control Flow Graph (CFG) for the code. 2. (10 points) Enumerate the Test Requirements for node coverage. Design a set of 3. 4. 5. test cases (input value num) that will satisfy node coverage. (10 points) Enumerate the Test Requirements for edge coverage. Design a set of test...
#include <iostream>
#include <queue>
using namespace std;
class Graph {
public:
Graph(int n);
~Graph();
void addEdge(int src, int tar);
void BFTraversal();
void DFTraversal();
void printVertices();
void printEdges();
private:
int vertexCount;
int edgeCount;
bool** adjMat;
void BFS(int n, bool marked[]);
void DFS(int n, bool marked[]);
};
Graph::Graph(int n=0) {
vertexCount = n;
edgeCount = 0;
if(n == 0)
adjMat = 0;
else {
adjMat = new bool* [n];
for(int i=0; i < n; i++)
adjMat[i] = new bool [n];
for(int i=0;...
(c) Simulate breadth first search on the graph shown in Fig
HW2Q1c. You can assume that the starting vertex is 1, and the
neighbors of a vertex are examined in increasing numerical order
(i.e. if there is a choice between two or more neighbors, we pick
the smaller one). You have to show: both the order in which the
vertices are visited and the breadth first search tree. No
explanations necessary.
(d) On the same graph, i.e. the graph in...
Here is the IntegerLinkedList_incomplete
class:
public class IntegerLinkedList {
static class Node {
/** The element stored at this node */
private int element; // reference to the element stored at this node
/** A reference to the subsequent node in the list */
private Node next; // reference to the subsequent node in the list
/**
* Creates a node with the given element and next node.
*
* @param e the element to be stored
* @param n...
1. Write a function in Tree class which returns true if and only if the tree satisfies the binary search tree property. The function’s header line is public boolean isValidBST() And in the attached code, you just need to finish the function after the comment: “//Instructor hint: please write your code here:” Make sure you execute your code, and the result in the main function after calling your function should be same as the prompt message I write. Clearly you...
(5 marks) a. The pseudo-code for breadth-first search, modified slightly from Drozdek,1 is as follows: void breadthFirstSearch (vertex w) for all vertices u num (u) 0 null edges i=1; num (w) i++ enqueue (w) while queue is not empty dequeue ( V= for all vertices u adjacent to v if num(u) is 0 num (u) = i++; enqueue (u) attach edge (vu) to edges; output edges; Now consider the following graph. Give the breadth-first traversal of the graph, starting from...