Question

C++ In this we introduce direction graphs or digraphs. Digraphs are just like regular graphs except...

C++ In this we introduce direction graphs or digraphs. Digraphs are just like regular graphs except each edge also contains a direction: in other words if you have two vertices a and b, just because a is connected to b doesn't necessarily mean b is connected to a. As a result, we don't just have a degree sequence, each vertex has an in-degree and out-degree. Intuitively, the in-degree is how many edges going into a vertex, an the out-degree is the number of edges leaving the vertex. Note an edge may be leaving and entering at the same time. We would like to extend Havel-Hakimi to the domain of digraphs. The algorithm is very similar however we consider instead of the degree sequence, a sequence of pairs, where each pair is of the form (a, b), with a being the out-degree and b being the in-degree. This sequence is sorted from largest to smallest by the out-degree first, and then in-degree, so consider the graph above. The sequence will be as follows (clockwise starting upper left). {(1, 1), (1, 2), (2, 1)}. If we sort this as described above our sequence becomes {(2, 1), (1, 2), (1, 1)}. The Havel-Hakimi algorithm then sets the out-degree of the first node to 0, whatever number that is, say x, it then subtracts 1 from the next x vertices' in-degree skipping zeros, and then resorts. Again, an empty list results in true, and any list of all 0s results in true. Also again, if the subtraction operation results in 'extra' values not being subtracted (e.g. need to subtract 5 times but the list is only of length 4),that also results in false. An example of the algorithm of the above graph is as follows:

Base case: {(2, 1), (1, 2), (1, 1)}

The out-degree is 2 so after setting the first vertex to 0 we get {(0, 1), (1, 2), (1, 1)}

Subtracting 1 from the next 2 vertices we get: {(0, 1), (1, 1), (1, 0)}

Resort: {(1, 1), (1, 0), (0, 1)}

Repeat the setting of 0, saving the 1: {(0, 1), (1, 0), (0, 1)}

Subtract 1 from the next 1 vertex skipping 0s: {(0, 1), (1, 0), (0, 0)}

Resort: {(1, 0), (0, 1), (0, 0)} {(0, 0), (0, 1), (0, 0)} {(0 0), (0, 0), (0, 0)}

Which leads to an all 0 sequence which is trivially true.

For this homework write a function called havel_hakimi_digraph that takes in a vector<pair<int, int>> and then returns whether or not the graph is graphical.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool com(pair<int, int> x, pair<int, int> y)//comparator to sort the degree list in the required order
{
if(x.first==y.first)return x.second>y.second;
return x.first>y.first;
}
bool havel_hakimi_diagraph(vector <pair<int, int>> v)//function
{
bool res=true;//this will store the result
sort(v.begin(), v.end(), com);//sorting the list
int n=v.size();
while(v[0].first!=0)
{
int val=v[0].first;//doing exactly what said in the problem
v[0].first=0;//setting the highest out degree to zero
for(int i=1;i<n;i++)
{
if(v[i].second)//subtracting 1 if it is possible else skip it
{
v[i].second--;
val--;
}
if(val==0)break;//if it is already done break
}
if(val!=0)//this means there was no vertex with a positive in degree left
{
return false;//so graph is not possible
}
sort(v.begin(), v.end(), com);
}//coming out of the loop
//running the test to check if given graph is graphical
for(int i=0;i<n;i++)
{
if(v[i].first!=0||v[i].second!=0)//this means all the elements of this list must be zero now
{
res=false;//if any of the element is non zero then graph is not possible
break;
}
}
return res;//return the result
}
int main()
{
int n, a, b;
cout<<"Enter number of nodes: ";
cin>>n;//taking the inputs
vector <pair<int, int>> v;
for(int i=0;i<n;i++)
{
cin>>a>>b;
v.push_back(make_pair(a, b));
}
//printing the results
if(havel_hakimi_diagraph(v))
{
cout<<"The given graph is graphical"<<endl;
}
else
{
cout<<"The given graph is not graphical"<<endl;
}
return 0;
}

SCCREENSHOT:

OUTPUT:

NOTE:

PLEASE GIVE ME UP VOTE THANK YOU :)

Add a comment
Know the answer?
Add Answer to:
C++ In this we introduce direction graphs or digraphs. Digraphs are just like regular graphs except...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Task 3: Grid Graphs and Mazes Part A - Generating Grid Graphs In the lecture we...

    Task 3: Grid Graphs and Mazes Part A - Generating Grid Graphs In the lecture we said that we can use Prim's algorithm to create mazes by starting from a regular "grid graph and then finding a spanning tree. Implement a function grid graph (m, n) that takes as input two positive integers, and returns the adjacency matrix of a m by n grid graphie, a graph with ses vertices that, when drawn on a regular m by n grid,...

  • 1. In the following graph, suppose that the vertices A, B, C, D, E, and F represent towns, and th...

    question 1 and 2 please, thank you. 1. In the following graph, suppose that the vertices A, B, C, D, E, and F represent towns, and the edges between those vertices represent roads. And suppose that you want to start traveling from town A, pass through each town exactly once, and then end at town F. List all the different paths that you could take Hin: For instance, one of the paths is A, B, C, E, D, F. (These...

  • 2. (Graphs, degree sequence) If G is a simple graph with n vertices, then the degree...

    2. (Graphs, degree sequence) If G is a simple graph with n vertices, then the degree sequence of G is a list a1, a2, a3, . . . , an of the degrees of all of the vertices of G in decreasing order. For instance, the degree sequence of the graph G drawn here is 3, 2, 2, 2, 2, 2, 1, 0. (a) Sketch a graph with the degree sequence 4, 3, 2, 2, 2, 1, and a graph...

  • 47.21. What does it mean for two graphs to be the same? Let G and H be graphs. We say th G is iso...

    please throughly explain each step.47.21. What does it mean for two graphs to be the same? Let G and H be graphs. We say th G is isomorphic to H provided there is a bijection f VG)-V(H) such that for all a, b e V(G) we have a~b (in G) if and only if f(a)~f (b) (in H). The function f is called an isomorphism of G to H We can think of f as renaming the vertices of G...

  • Please answer question 2. Introduction to Trees Thank you 1. Graphs (11 points) (1) (3 points)...

    Please answer question 2. Introduction to Trees Thank you 1. Graphs (11 points) (1) (3 points) How many strongly connected components are in the three graphs below? List the vertices associated with each one. 00 (2) (4 points) For the graph G5: (a) (0.5 points) Specify the set of vertices V. (b) (0.5 points) Specify the set of edges E. (c) (1 point) Give the degree for each vertex. (d) (1 point) Give the adjacency matrix representation for this graph....

  • In C++ Design a format for storing graphs in files. This will store your graph into a file with t...

    in C++ Design a format for storing graphs in files. This will store your graph into a file with the following requirements: The first line will contain the number of Vertices. The second line will have a ‘U’ or a ‘D’ to tell the system if it is Undirected or Directed. The rest of the lines will be here for each of the edges. Each edge will contain three pieces of information: An integer containing the first Vertex An integer...

  • Consider the following weighted, directed graph G. There are 7 vertices and 10 edges. The edge list E is as follows:

    Consider the following weighted, directed graph G. There are 7 vertices and 10 edges. The edge list E is as follows:The Bellman-Ford algorithm makes |V|-1 = 7-1 = 6 passes through the edge list E. Each pass relaxes the edges in the order they appear in the edge list. As with Dijkstra's algorithm, we record the current best known cost D[V] to reach each vertex V from the start vertex S. Initially D[A]=0 and D[V]=+oo for all the other vertices...

  • Viterbi algorithm We can use dynamic programming on a directed graph G = (V, E) for...

    Viterbi algorithm We can use dynamic programming on a directed graph G = (V, E) for speech recognition. Each edge (u, v) in E is labeled with a sound s(u, v) from a finite set S of sounds. The labeled graph is a formal model of a person speaking a restricted language. Each path in the graph starting from a distinguished vertex v0 in V corresponds to a possible sequence of sounds produced by the model. The label of a...

  • Say that we have an undirected graph G(V, E) and a pair of vertices s, t and a vertex v that we call a a desired middle vertex . We wish to find out if there exists a simple path (every vertex appears...

    Say that we have an undirected graph G(V, E) and a pair of vertices s, t and a vertex v that we call a a desired middle vertex . We wish to find out if there exists a simple path (every vertex appears at most once) from s to t that goes via v. Create a flow network by making v a source. Add a new vertex Z as a sink. Join s, t with two directed edges of capacity...

  • What does it mean for two graphs to be the same? Let G and H be...

    What does it mean for two graphs to be the same? Let G and H be graphs. We Say that G is isomorphic to H provided there is a bijection f : V(G) rightarrow V(H) such that for all a middot b epsilon V(G) we have a~b (in G) if and only if f(a) ~ f(b) (in H). The function f is called an isomorphism of G to H. We can think of f as renaming the vertices of G...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT