Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a c++ program that can accomplish the following: 1. Read in a graph from g

ID: 3773241 • Letter: W

Question

Write a c++ program that can accomplish the following:

1. Read in a graph from graph.dat in the following format:

node# 1st-arc-to-node# weight-for-previous-arc 2nd-arc-to-node# weight-for-previous-arc 3rd-arc-to-node# weight-for-previous-arc ...

As an example:

1 2 10 3 5 4 100
2 1 5
4 3 50 2 10

In the above example, node 1 has an arc with weight 10 to node 2, weight 5 to node 3, and weight 100 to node 4. Node 2 has an arc with weight 5 to node 1. Node 4 has an arc to node 3 with weight 50 and to node 2 with weight 10.
Note that in the above example, there IS a node 3 despite not being specifically declared on a line. Any node that is either explicitly defined on its own line, or into which an arc goes, must be considered to exist.

2. Display an ASCII art image of the graph on the screen. An sample for the above example is shown below. It is not the only possible example:

    +-----5-------->[3]

    |                ^

    |                |

    |                50

    |                |

+->[1]--10-->[2]<-10-+

|   |         |      |

+---=---5-----+      |

    |                |

   100               |

    |                |

    V                |

   [4]---------------+

As can be seen in the above example, use the following format:

nodes are written inside brackets

vertical lines are drawn with |

horizontal lines are drawn with -

90 degree bends, and intersections between lines (in order to share one line between two arcs) are drawn with +

a horizontal line crossing but not intersecting with a vertical line is drawn with an =

arc weights are written somewhere on the arc's line; if a line is shared between two arcs, only the part of the line used by only ONE arc may show the arc weight

to draw arrows, use <, >, V, and ^.

You do NOT need to optimize for the least number of crosses, turns, or intersections. You DO need to account for every possible graph where id(v) + od(v) <= 4 for all nodes v.

3. Display a chart, in an easily readable format, of the shortest path between each node s and every node t reachable from node s.

Explanation / Answer

#include <iostream.h>   // input output stream header file
#include <stdio.h>   // standard input output header file
#include <stdlib.h>     // standard library header file
//#include <string.h>   // header file with string function
// #include <conio.h>   // console input output header file
int main()   // start main
{
   FILE fp;
   fp = fopen("E:/Files/graph.dat", "r");
   int fromnode1, fromnode2, fromnode3, fromnode4, fromnode5, fromnode6, fromnode7;
   int wt1, wt2, wt3, wt4, wt5, wt6, wt7, wt8, wt9, wt10;
   int toNode1, toNode2, toNode3, toNode4, toNode5, toNode6, toNode7, toNode8, toNode9, toNode10;

   while (!feof(fp))   {
       fscanf(fp, "%d", &fromnode1);      
       fscanf(fp, "%d", &wt1);
       fscanf(fp, "%d", &toNode1);
   }

   cout << " ";
   cout << "+-----> [ " << fromnode1 << " ] ------ " << wt1 << "------> " << toNode1;
   cout << "+-----> [ " << fromnode2 << " ] ------ " << wt2 << "------> " << toNode2;
   cout << "+-----> [ " << fromnode3 << " ] ------ " << wt3 << "------> " << toNode3;
   cout << "+-----> [ " << fromnode4 << " ] ------ " << wt4 << "------> " << toNode4;
   cout << "+-----> [ " << fromnode5 << " ] ------ " << wt5 << "------> " << toNode5;
   cout << "+-----> [ " << fromnode6 << " ] ------ " << wt6 << "------> " << toNode6;
   cout << "+-----> [ " << fromnode7 << " ] ------ " << wt7 << "------> " << toNode7;
   cout << "+-----> [ " << fromnode8 << " ] ------ " << wt8 << "------> " << toNode8;
   cout << "+-----> [ " << fromnode9 << " ] ------ " << wt << "------> " << toNode9;
   cout << "+-----> [ " << fromnode10 << " ] ------ " << wt10 << "------> " << toNode10;


return 0; // exit
} // end of main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote