Question

In: Computer Science

// (A) Implement the code below. For a given graph, return a Set of type T...

// (A) Implement the code below. For a given graph, return a Set of type T of all of the strongly connected nodes of the given key.

public Set<T> stronglyConnectedComponent(T key) {

return null;

}

// (B) Implement the code below. Optimally find the shortest path between the given start and end node.

public List<T> shortestPath(T startLabel, T endLabel) {
       return null;
}

Solutions

Expert Solution

(A) Implement the code below. For a given graph, return a Set of type T of all of the strongly connected nodes of the given key.

Program to check if a given directed graph is strongly connected


#include <iostream>
#include <stack>

#include <list>

using namespace std;

class Graph
{
   int V; // No. of vertices
   list<int> *adj; // An array of adjacency lists
   void DFSUtil(int v, bool visited[]);
public:
   Graph(int V) { this->V = V; adj = new list<int>[V];}
   ~Graph() { delete [] adj; }
   void addEdge(int v, int w);


   bool isStronglyConnected();
   Graph getTranspose();
};
void Graph::DFSUtil(int v, bool visited[])
{

   visited[v] = true;
   list<int>::iterator i;
   for (i = adj[v].begin(); i != adj[v].end(); ++i)
       if (!visited[*i])
           DFSUtil(*i, visited);
}
Graph Graph::getTranspose()
{
   Graph g(V);
   for (int v = 0; v < V; v++)
   {

       list<int>::iterator i;
       for(i = adj[v].begin(); i != adj[v].end(); ++i)
       {
           g.adj[*i].push_back(v);
       }
   }
   return g;
}

void Graph::addEdge(int v, int w)
{
   adj[v].push_back(w);
}


bool Graph::isStronglyConnected()
{
  
   bool visited[V];
   for (int i = 0; i < V; i++)
       visited[i] = false;


   DFSUtil(0, visited);


   for (int i = 0; i < V; i++)
       if (visited[i] == false)
           return false;


   Graph gr = getTranspose();
   for(int i = 0; i < V; i++)
       visited[i] = false;


   gr.DFSUtil(0, visited);


   for (int i = 0; i < V; i++)
       if (visited[i] == false)
           return false;

   return true;
}


int main()
{
  
   Graph g1(5);
   g1.addEdge(0, 1);
   g1.addEdge(1, 2);
   g1.addEdge(2, 3);
   g1.addEdge(3, 0);
   g1.addEdge(2, 4);
   g1.addEdge(4, 2);
   g1.isStronglyConnected()? cout << "Yes\n" : cout << "No\n";

   Graph g2(4);
   g2.addEdge(0, 1);
   g2.addEdge(1, 2);
   g2.addEdge(2, 3);
   g2.isStronglyConnected()? cout << "Yes\n" : cout << "No\n";

   return 0;
}

(B) Implement the code below. Optimally find the shortest path between the given start and end node.


#include <bits/stdc++.h>
using namespace std;


void add-edge(vector<int> adj[], int src, int dest)
{
   adj[src].push_back(dest);
   adj[dest].push_back(src);
}


bool BFS(vector<int> adj[], int src, int dest, int v,
                           int pred[], int dist[])
{
   list<int> queue;

   bool visited[v];

   for (int i = 0; i < v; i++) {
       visited[i] = false;
       dist[i] = INT_MAX;
       pred[i] = -1;
   }

  
   visited[src] = true;
   dist[src] = 0;
   queue.push_back(src);

  
   while (!queue.empty()) {
       int u = queue.front();
       queue.pop_front();
       for (int i = 0; i < adj[u].size(); i++) {
           if (visited[adj[u][i]] == false) {
               visited[adj[u][i]] = true;
               dist[adj[u][i]] = dist[u] + 1;
               pred[adj[u][i]] = u;
               queue.push_back(adj[u][i]);
               if (adj[u][i] == dest)
               return true;
           }
       }
   }

   return false;
}
void printShortestDistance(vector<int> adj[], int s,
                                   int dest, int v)
{
   int pred[v], dist[v];

   if (BFS(adj, s, dest, v, pred, dist) == false)
   {
       cout << "Given source and destination"
           << " are not connected";
       return;
   }
   vector<int> path;
   int crawl = dest;
   path.push_back(crawl);
   while (pred[crawl] != -1) {
       path.push_back(pred[crawl]);
       crawl = pred[crawl];
   }


   cout << "Shortest path length is : "
       << dist[dest];

  
   cout << "\nPath is::\n";
   for (int i = path.size() - 1; i >= 0; i--)
       cout << path[i] << " ";
}


int main()
{

   int v = 8;


   vector<int> adj[v];

  
   add_edge(adj, 0, 1);
   add_edge(adj, 0, 3);
   add_edge(adj, 1, 2);
   add_edge(adj, 3, 4);
   add_edge(adj, 3, 7);
   add_edge(adj, 4, 5);
   add_edge(adj, 4, 6);
   add_edge(adj, 4, 7);
   add_edge(adj, 5, 6);
   add_edge(adj, 6, 7);
   int source = 0, dest = 7;
   printShortestDistance(adj, source, dest, v);
   return 0;
}



Related Solutions

A data set is given below. ​(a) Draw a scatter diagram. Comment on the type of...
A data set is given below. ​(a) Draw a scatter diagram. Comment on the type of relation that appears to exist between x and y. ​(b) Given that x̅ = 3.8333​, Sx = 2.4014 ​, ȳ equals = 3.7333 3.​, Sy = 1.8381​, and r = -0.9545 ​, determine the​ least-squares regression line. (c) Graph the​ least-squares regression line on the scatter diagram drawn in part x   y 0   5.9 2   5.7 4   4.3 5   2.8 6   1.7 6   2...
A data set is given below. ​(a) Draw a scatter diagram. Comment on the type of...
A data set is given below. ​(a) Draw a scatter diagram. Comment on the type of relation that appears to exist between x and y. ​(b) Given that x̅ = 3.6667, sx = 2.0656​, ŷ = 4.2000​, sy = 1.4805, and r = −0.9287​, determine the​ least-squares regression line. ​(c) Graph the​ least-squares regression line on the scatter diagram drawn in part​ (a). x   y 1   5.2 2   5.8 3   5.4 4   3.8 6   2.4 6   2.6 ​(a) Choose the...
Complete the below code so that your program generates a random walk on the given graph....
Complete the below code so that your program generates a random walk on the given graph. The length of your walk should also be random. /******************************************************************/ #include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct NODE_s *NODE; typedef struct NODE_s{ char name; NODE link[10]; }NODE_t[1]; #define nodeA 0 #define nodeB 1 #define nodeC 2 #define nodeD 3 #define nodeE 4 #define nodeF 5 int main() { srandom(time(NULL)); //printf("%d\n", random()); NODE_t node[6]; node[nodeA]->name = 'A'; node[nodeB]->name = 'B'; node[nodeC]->name = 'C'; node[nodeD]->name...
Just Implement Inorder, Preorder and Postorder of Tree Data Structure in given code below. Thank You....
Just Implement Inorder, Preorder and Postorder of Tree Data Structure in given code below. Thank You. #include<bits/stdc++.h> using namespace std; struct Node{     int data;     Node* left;     Node* right; }; Node* createNode(int value){     Node* newNode = new Node();     newNode->data = value;     newNode->left = NULL;     newNode->right = NULL;     return newNode; } Node* insert(Node *currentNode, int value){     if(currentNode==NULL){         currentNode = createNode(value);     }else if(value <= currentNode->data){         currentNode->left = insert(currentNode->left,value);     }else{        ...
The code below is giving an arithmetic overflow. Correct the below given the below code, so...
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box. .text main: addi $s0,$zero,2 jal Disp jal Fun1 jal Disp j Exit Fun1: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$s0,15 jal Disp lw $s0,0($sp) addi $sp,$sp,4 jr $ra Disp: li $v0,1...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
Question 1: A small data set is given below. You will run a t-test using Excel....
Question 1: A small data set is given below. You will run a t-test using Excel. In order to receive full credit, you must turn in Excel output and your final answers. Round off final answers to three decimal places, if appropriate. A medical researcher wants to determine whether a drug changes the body’s temperature. Seventest subjects are randomly selected, and the body temperature (in degrees Fahrenheit) of each is measured. The subjects are then given the drug and after...
Direction: A small data set is given below. You will run a t-test using Excel. You...
Direction: A small data set is given below. You will run a t-test using Excel. You must provide Excel output to get full credit for this question. A medical researcher wants to determine whether a drug changes the body’s temperature. Seven test subjects are randomly selected, and the body temperature (in degrees Fahrenheit) of each is measured. The subjects are then given the drug and after 20 minutes, the body temperature of each is measured again. The results are listed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT