This code is using a graph with edge sets . Create an application that uses a gr
ID: 3568975 • Letter: T
Question
This code is using a graph with edge sets. Create an application that uses a graph with an adjacency matrix instead.
---------------------------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
#include <set>
typedef unsigned int UINT;
#define SIZE 4
int main()
{
set<UINT> vertices[SIZE];
vertices[0].insert(0); vertices[0].insert(2);
vertices[1].insert(0); vertices[1].insert(3);
vertices[2].insert(3);
vertices[3].insert(0); vertices[3].insert(2);
set<UINT>::iterator iter;
for (int i = 0; i < SIZE; i++)
{
cout << i << ": ";
for (iter = vertices[i].begin();iter != vertices[i].end();iter++)
cout << *iter << " ";
cout << "----------- ";
}
int first, second;
while (true)
{
cout << "Enter -1 to quit ";
cout << "From vertex (enter number from 0 to " << SIZE-1 << "): ";
cin >> first;
if (first >= SIZE || first < -1)
{
cout << "Illegal entry ";
continue;
}
if (first == -1) break;
cout << "From vertex " << first << " to ? (enter number from 0 to "
<< SIZE - 1 << "): ";
cin >> second;
if (second >= SIZE || second < 0)
{
cout << "Illegal entry ";
continue;
}
iter = vertices[first].find(second); // very efficient as sets use a tree
if (iter != vertices[first].end())
cout << "Found " << *iter << endl;
else
cout << "Did not find " << second << endl;
}
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
#include using namespace std; const int t = 100; class graph { struct edge { int distance; int name; bool connection; }; struct edge adj[t][t]; int v; public: graph(); void vertices ( int ver ); void add(int distance , int v1 , int v2); void del(int v1 ,int v2); void display(); }; graph::graph() { adj[v][v]; for (int i = 0 ; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.