in C++
Design a format for storing graphs in files. This will store your graph into a file with the following requirements:
You will need to write two functions for your graph, read and write.
The file must be human readable (use a txt file please).
Here is a sample input file.
Input.txt
6
# Number of vertices
U
# Undirected graph
0 4 9 #
List of Edges
0 2 7
2 3 1
2 1 5
2 5 2
CPP CODE:
#include<iostream>
#include<fstream>
using namespace std;
bool write(string filename)
{
ofstream fout;
fout.open(filename.c_str());
cout<<"Enter number of vertices in graph: ";
int n;
cin>>n;
fout<<n<<endl;
cout<<"Enter U for undirected and D for directed graph:
";
char c;
cin>>c;
fout<<c<<endl;
cout<<"Now add edges by writing first vertex followed by
second vertex followed by weight"<<endl;
cout<<"Enter 0 0 0 to end"<<endl;
int graph[n][n];
while(1)
{
int v1,v2,w;
cin>>v1>>v2>>w;
if(v1==0&&v2==0&&w==0)
break;
fout<<v1<<" "<<v2<<"
"<<w<<endl;
}
fout.close();
return true;
}
bool read(string filename)
{
ifstream fin;
fin.open(filename.c_str());
cout<<"Reading from file"<<endl;
int n;
fin>>n;
cout<<"Vertices in graph: "<<n<<endl;
char c;
fin>>c;
if(c=='U')
cout<<"Undirected Graph"<<endl;
else
cout<<"Directed Graph"<<endl;
cout<<"V1\tV2\tWeight"<<endl;
int v1,v2,w;
while(fin)
{
fin>>v1>>v2>>w;
cout<<v1<<"\t"<<v2<<"\t"<<w<<endl;
}
}
int main()
{
string filename="graph.txt";
if(write(filename))
cout<<"Graph written in file successfully"<<endl;
if(read(filename))
cout<<"Graph read from file successfully"<<endl;
return 0;
}
Output:
File: graph.txt
In C++ Design a format for storing graphs in files. This will store your graph into a file with t...
Creating Graphs First, write Java code in GraphTest.java to read a data file (graph.in) and create a graph using the input data. The input file must have the following format: 6 049 0 27 2 3 1 25 2 1 5 6 352 4 5 1 Here, the number on the first line represents the number of vertices in the graph. The letter on the second line indicates whether this is a directed graph ('D') or an undirected graph ('U')....
Write down true (T) or false (F) for each statement. Statements are shown below If a graph with n vertices is connected, then it must have at least n − 1 edges. If a graph with n vertices has at least n − 1 edges, then it must be connected. If a simple undirected graph with n vertices has at least n edges, then it must contain a cycle. If a graph with n vertices contain a cycle, then it...
Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, to implement the depth-first search algorithm using the pseudocode given. Write a driver program, which reads input file mediumG.txt as an undirected graph and runs the depth-first search algorithm to find paths to all the other vertices considering 0 as the source. This driver program should display the paths in the following manner: 0 to ‘v’: list of all the vertices traversed to...
In java, how can I convert this text file format: n=6 m=7 1 2 5 4 9 5 6 2 3 2 3 4 3 5 6 2 6 4 2 // end of file into this text file format: 1,2,5 1,4,9 1,5,3 2,3,2 3,4,3 5,6,2 6,4,2 This is how to read the first text file: The first line of each file below contains the number of vertices and the number of edges in the graph (in the format "n=XXXX...
7. Graphs u, u2, u3, u4, u5, u6} and the (a) Consider the undirected graph G (V, E), with vertex set V set of edges E ((ul,u2), (u2,u3), (u3, u4), (u4, u5), (u5, u6). (u6, ul)} i. Draw a graphical representation of G. ii. Write the adjacency matrix of the graph G ii. Is the graph G isomorphic to any member of K, C, Wn or Q? Justify your answer. a. (1 Mark) (2 Marks) (2 Marks) b. Consider an...
Java Programming 1) zipped project folder 2) executable jar file 3) UML and running results screen shot in a word file with names of your team 5 points will be taken for each missing file Problem Description A graph consists of vertices and edges that connect vertices Write a program that reads a graph from a file and displays it on a panel. The first line in the file contains a number that indicates the number of vertices (n) The...
Please provide C language code no c++ ,txt file also needed with comment Finish task 5 Task5: Breadth First Search (15 pts) · Write a program to read the graph information from the file and traverse the graph using BFS algorithm as introduced in lecture. The input for each algorithm is an undirected unweighted connected graph stored in a local file using an adjacency list. Following is the example of the input file (graph.txt) and the graph First line is...
Using java: Store the files in .txt format and use the readByte() and writeByte() methods. File encryption is the science of writing the contents of a file in a secret code. Write an encryption program that works like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code....
You will be implementing a Breadth-First Search (BFS) and a Depth-First Search (DFS) algorithm on a graph stored as an adjacency list. The AdjacencyList class inherits from the Graph class shown below. class Graph { private: vector _distances; vector _previous; public: Graph() { } virtual int vertices() const = 0; virtual int edges() const = 0; virtual int distance(int) const = 0; virtual void bfs(int) const = 0; virtual void dfs(int) const = 0; virtual void display() const = 0;...
Below is the Graph file that needs to be modified(using Python3) : #!/usr/bin/python3 # Simple Vertex class class Vertex: """ Lightweight vertex structure for a graph. Vertices can have the following labels: UNEXPLORED VISITED Assuming the element of a vertex is string type """ __slots__ = '_element', '_label' def __init__(self, element, label="UNEXPLORED"): """ Constructor. """ self._element = element self._label = label def element(self): """ Return element associated with this vertex. """ return self._element def getLabel(self): """ Get label assigned to...