Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you help me with GetGraph? C++ only. Retrieves a graph from a special file and sets up the adjacency list for the graph. I

Can you help me with GetGraph? C++ only.

Retrieves a graph from a special file and sets up the adjacency list for the graph. I am supplying 1 file. The program will be able to read any graph that is in the same format: graph node followed by any adjacent nodes followed by distance/weight to the node. The adjacency entry is terminated by #. I want to have both of the files to be in the function.

GetGraph //*********************************************************** // Function Name: GetGraph // Purpose: get a file that has a graph representing as adjacency list // Functions Called: AddVertex, AddUniEdge, AddBiDirEdge //************************************************************ template void Graph::GetGraph() { edgeRep G1; vertex dist; ifstream input, input2; string line; input.open("graphs2.txt"); while (!input.eof()) { input >> line; AddVertex(dist.name); while (input >> line != '#') { input >> line; AddUniEdge(G1.name, G1.weight); } } input.close(); input2.open("graph3.txt"); while (!input.eof()) { input >> line; AddVertex(dist.name); while (input >> line != '#') { input >> line; AddBiDirEdge(dist.name, G1.name, G1.weight); } } input2.close(); }

This is a c++ only.

Graph2.txt (bidirected)

Atlanta Houston 650 Washington 600 # Austin Dallas 200 Houston 300 # Buffalo New_York 450 Newark 500 # Chicago Denver 550 New_York 950 # Dallas Austin 200 Chicago 1500 # Denver Atlanta 800 Chicago 550 # Houston Atlanta 650 # Newark Atlanta 1100 # New_York Chicago 950 Buffalo 450 # Washington Atlanta 600 Dallas 700 #

Graph3.txt (directed graph)

Atlanta Houston 650 Washington 600 # Austin Dallas 200 Houston 300 # Buffalo New_York 450 Newark 500 # Chicago Denver 550 New_York 950 # Dallas Austin 200 Chicago 1500 # Denver Atlanta 800 Chicago 550 # Houston Atlanta 650 # Newark Atlanta 1100 # New_York Chicago 950 Buffalo 450 # Washington Atlanta 600 Dallas 700 #

EdgeRep

template // V is the vertex class; W is edge weight class struct edgeRep { V name; // Vertex name W weight; // Edge weight };

AddUniEdge

template int Graph::AddUniEdge(V& v1, V& v2, W &wt) { for (int i = 0; i < MAX; i++) { if (G[i] == NULL) { G[i] = v1; G[i + 1] = v2, wt; return 1; } } return 0; }

AddBiDirEdge

template int Graph::AddBiDirEdge(V& v1, V& v2, W& wt) { for (int i = 0; i < MAX; i++) { if (G[i] == NULL) { G[i] = v2; G[i + 1] = v1, wt; return 1; } } return 0; }

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

Successful Keyword Searching Initiating Research On Popular Topics Using Electronic Databases

Authors: Randall MacDonald, Susan MacDonald

1st Edition

0313306761, 978-0313306761

More Books

Students also viewed these Databases questions

Question

How is slaked lime powder prepared ?

Answered: 1 week ago

Question

3 Balance the following equation. Fe(s)+HCl(aq)FeCl2(aq)+H2(g)

Answered: 1 week ago

Question

What are the purposes of promotion ?

Answered: 1 week ago