2. Let G = (V, E) be a directed graph where V = {1, 2, .. . ,n) such that n is o
ID: 3607211 • Letter: 2
Question
2. Let G = (V, E) be a directed graph where V = {1, 2, .. . ,n) such that n is odd: Le n = 2k +1 for some k 0. Given a vertex v, let TO be the set of all vertices from which there is path to v. Let FROM be the set al vertices for which there is a path from v. Le, TO' = (1 1 There is a path from u to u), FROM,-(vl There is a path from u to w} A vertex v is center verter of G if all of the following conditions hold: . ITO,I = FROM,l = k. Le, both TOU and FROMu have exactly k vertices. . Tan FROM, = 0. Le, TOU and FROM, are disjoint. Give an algorithm that gets a graph G (with odd number of vertices) as input and determines if the graph has a center vertex or not. If the graph has a center vertex, then the algorithm must output it. Describe your algorithm, prove the correctness, and derive the time bound Your grade partly depends on the efficiency of your algorithmExplanation / Answer
Ans:From the given data in question ,V={1,2....,n},n is odd and n=2k+1,k>0;
The algorithm is given as follows:
#include <iostream>
#include <queue>
#define V 4
using namespace std;
bool containsOdd(int G[][V], int src)
{
int colorArr[V];
for (int i = 0; i < V; ++i)
colorArr[i] = -1;
colorArr[src] = 1;
queue <int> q;
q.push(src);
while (!q.empty())
{
int u = q.front();
q.pop();
if (G[u][u] == 1)
return true;
for (int v = 0; v < V; ++v)
{
if (G[u][v] && colorArr[v] == -1)
{
colorArr[v] = 1 - colorArr[u];
q.push(v);
}
else if (G[u][v] && colorArr[v] == colorArr[u])
return true;
}
}
return false;
}
int main()
{
int G[][V] = {{0, 1, 0, 1},
{1, 0, 1, 0},
{0, 1, 0, 1},
{1, 0, 1, 0}
};
containsOdd(G, 0) ? cout << "Yes" : cout << "No";
return 0;
}
In this algorithm we can get the graph is odd cycle or not with checking whether graph is bipartite or not.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.