[Solved] CSCI 2270 Homework8 -Graph Class

$25

File Name: CSCI_2270_Homework8_-Graph_Class.zip
File Size: 301.44 KB

SKU: [Solved] CSCI 2270 Homework8 -Graph Class Category: Tag:
5/5 - (1 vote)

In this assignment, you will apply BFS for finding connected cities in a graph and check whether a graph is Bipartite graph or not.

Graph Class

Your code should implement graph traversal for cities. A header file that lays out this graph can be found in Graph.hpp on Moodle. As usual, do not modify the header file. You may implement helper functions in your .cpp file if you want as long as you dont add those functions to the Graph class.

Your graph will utilize the following struct:

struct vertex; struct adjVertex{vertex *v;}; struct vertex{vertex() {this->visited = false;this->color = ;this->distance = 0;}string name;bool visited;string color;int distance;vector<adjVertex> adj;};

void addVertex(string name);

  • Add new vertex name to the graph.

void addEdge(string v1, string v2);

  • Make a connection between v1 and v2.

void displayEdges();

  • Display the all the edges in the graph.

Format for printing:

If we create a graph with the following structure

graph.addVertex(Boulder);graph.addVertex(Denver);graph.addVertex(Las Vegas); graph.addEdge(Boulder, Denver);graph.addEdge(Las Vegas, Denver);

We print the edges in the following manner.

Boulder > Denver Denver > Boulder Las VegasLas Vegas > Denver

The order of vertices printed is the same as the order in which they were added to the graph. Similarly, the order of vertices to the right of > sign is the same as the order in which the corresponding edge was added to the graph.

void breadthFirstTraverse(string sourceVertex);

  • Breadth first traversal from sourceVertex. Format for printing:
// for the source vertex in the graphcout<< Starting vertex (root): << vStart->name << ->;// for other vertex traversed from source vertex with distancecout << n->adj[x].v->name <<(<< n->adj[x].v->distance <<)<< ;

int getConnectedComponents();

  • This method will provide the number of connected components in the graph i.e. the number of distinct subgraphs where no edges exist which connect vertices in two distinct subgraphs. In the following graph, the number of connected components is 3.

bool checkBipartite();

  • This method will check whether a graph is bipartite or not. Will return true if the graph is Bipartite, otherwise false.

A Bipartite graph is a graph whose vertices can be divided into two sets and there will not be any edge among the vertices in the same set. We can say a graph is bipartite, if we can color the graph with two colors where vertices in the same set will be same colored.

In other words, in a Bipartite graph, no two adjacent vertices will have the same color.

In this graph, we color the vertices with two colors, so, its a bipartite graph. In this graph, if we try to color the vertices with two colors, we cant do that. As you can see, one vertex has one neighbor with the color red and the other with green. Therefore, for the graph to be bipartite, that node would have to be two colors at the same time. So, its not a bipartite graph.

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CSCI 2270 Homework8 -Graph Class
$25