Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Silver Badge Problem (Mandatory) 1. Download The Lab9 Zipped File. 2. Follow The TODOs And Complete The PrintGraph() Function Which Prints All The Graph Vertices

Silver Badge Problem (Mandatory) 1. Download The Lab9 Zipped File. 2. Follow The TODOs And Complete The PrintGraph() Function Which Prints All The Graph Vertices With Their Adjacency List. Graph.Cpp Graph.Cpp No Selection 11 14 1 #Include "Graph. Hpp" 2 #Include 3 #Include 4 #Include 5 6 Using Namespace Std; 7 8 // Function

3 #include 4 #include 5" style='width:537pt;height:768pt;visibility:visible;mso-wrap-style:square'>

========================///// Copy paste code ///////////////=======================

#include "Graph.hpp"

#include

#include

#include

usingnamespacestd;

// function to add edge between two vertices

voidGraph::addEdge(intv1,intv2){

for(inti = 0; i

if(vertices[i]->key == v1){

for(intj = 0; j

if(vertices[j]->key == v2 && i != j){

adjVertex av;

av.v = vertices[j];

vertices[i]->adj.push_back(av);

//another vertex for edge in other direction

adjVertex av2;

av2.v = vertices[i];

vertices[j]->adj.push_back(av2);

}

}

}

}

}

// function to add a vertex to the graph

voidGraph::addVertex(intn){

boolfound =false;

for(inti = 0; i

if(vertices[i]->key == n){

found =true;

}

}

if(found ==false){

vertex * v =newvertex;

v->key = n;

vertices.push_back(v);

}

}

//SILVER TODO

voidGraph::printGraph(){

// TODO

// loop through all vertices and their adjacent vertices

}

2 #include 3 #include Graph.hpp 4 5 using namespace std;" style='width:600pt;height:9in;visibility:visible;mso-wrap-style:square'> 2 #include 3 #include Graph">

========================///// Copy paste code ///////////////=======================

#include

#include

#include "Graph.hpp"

usingnamespacestd;

intmain()

{

Graph g; // undirected and unweighted

g.addVertex(1);

g.addVertex(2);

g.addVertex(3);

g.addVertex(4);

g.addVertex(5);

g.addVertex(6);

g.addEdge(1, 2);

g.addEdge(1, 3);

g.addEdge(2, 3);

g.addEdge(1, 4);

g.addEdge(4, 5);

g.addEdge(4, 6);

g.addEdge(3, 4);

g.addEdge(5, 6);

// SILVER TODO : Complete the printGraph() function

// Expected Format for printing (the vertices/adjacent vertices can be in any order):

// 1 --> 2 3 4

// 2 --> 1 3

// 3 --> 1 2 4

// 4 --> 1 3 5 6

// 5 --> 4 6

// 6 --> 4 5

g.printGraph();

return0;

}

4 #include 5" style='width:538.8pt;height:636.6pt;visibility:visible;mso-wrap-style:square'>

========================///// Copy paste code ///////////////=======================

#ifndef GRAPH_H

#define GRAPH_H

#include

#include

structvertex;

structadjVertex{

vertex *v;

};

structvertex{

intkey;

boolvisited =false;

std::vector adj;

};

classGraph

{

public:

voidaddEdge(intv1,intv2);

voidaddVertex(intv);

voidprintGraph();

private:

std::vector vertices;*>

};

#endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

App Inventor

Authors: David Wolber, Hal Abelson

1st Edition

1449397484, 9781449397487

Students also viewed these Programming questions