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 m=XXXXX"). The rest of the file is organized as follows: each vertex i appears on a line by itself, followed by a line for each neighbor j>i of i (containing j and the length of edge (i,j)). Each list of neighbors is ended by an empty line. Vertices i which do not have neighbors with an index greater than i are not represented. NOTE: Vertices are indexed from 0 to n-1. NOTE: each edge is mentioned only once with its smaller number endpoint Example: vertex 1 will have vertex neighbors 2, 4, and 5 with edges 5, 9, 6 respectivly.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileConversion {
public static void main(String[] args) throws IOException {
BufferedReader br = new
BufferedReader(new FileReader("D:\\file1.txt"));
try {
//String builder
for creating format
StringBuilder sb
= new StringBuilder();
//Read line by
line from text file
String line =
br.readLine();
String ver =
null;
while (line !=
null) {
//split each and every line by spaces
String[] strLine = line.toString().split("
");
// if line has edge
if (strLine.length == 1 &&
!strLine[0].equals("")) {
ver = strLine[0];
}
//if line has vertex
else if (ver != null && strLine.length
!= 1) {
String temp = ",";
for (String str : strLine)
{
//Condition to ignore white spaces
if
(!str.equals("")) {
temp = temp + str + ",";
}
}
sb.append(ver +
temp.substring(0, temp.length() - 1));
sb.append(System.lineSeparator());
}
line = br.readLine();
}
String
newContent = sb.toString();
//Write the new
content to file
BufferedWriter
bw = null;
FileWriter fw =
null;
try {
fw = new FileWriter("D:\\file2.txt");
bw = new BufferedWriter(fw);
bw.write(newContent);
} catch
(IOException e) {
}finally{
bw.close();
fw.close();
}
} finally {
br.close();
}
}
}
In java, how can I convert this text file format: n=6 m=7 1 2 5 4...
in C++ Design a format for storing graphs in files. This will store your graph into a file with the following requirements: The first line will contain the number of Vertices. The second line will have a ‘U’ or a ‘D’ to tell the system if it is Undirected or Directed. The rest of the lines will be here for each of the edges. Each edge will contain three pieces of information: An integer containing the first Vertex An integer...
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')....
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...
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...
how can I do this problem? thank you for the help it's much needed Conjecture and Prove: A convex polygon is a figure drawn in the plane consisting of edges (straight line segments) and vertices. Edges can only meet at vertices, each vertex has exactly two edges that meet there, and each of the interior angles measures less than 180°. A diagonal of a polygon is a line segment that connects two non-adjacent vertices (adjacent vertices share an edge between...
Please help me with this C++ I would like to create that uses a minimum spanning tree algorithm in C++. I would like the program to graph the edges with weights that are entered and will display the results. The contribution of each line will speak to an undirected edge of an associated weighted chart. The edge will comprise of two unequal non-negative whole numbers in the range 0 to 99 speaking to diagram vertices that the edge interfaces. Each...
32. (6 points) Trace the Text Search Algorithm for the input t"0101001' and p "001 Input: p( indexed from 1 to m),m, t (indexed from 1 to n),n Output: i text search(p,m,t,n) f For i= 1 to n-m+1( while (ti+/-1-= pj){ jj+1 If (j> m) Return i return 0 32. (6 points) Trace the Text Search Algorithm for the input t"0101001' and p "001 Input: p( indexed from 1 to m),m, t (indexed from 1 to n),n Output: i text...
I have a Graph.java which I need to complete four methods in the java file: completeGraph(), valence(int vid), DFS(int start), and findPathBFS(int start, int end). I also have a JUnit test file GraphTest.java for you to check your code. Here is Graph.java: import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /* Generic vertex class */ class Vertex<T> { public T data; public boolean visited; public Vertex() { data = null; visited = false; } public Vertex(T _data) { data =...
Solve all parts please 5. In the following problems, recall that the adjacency matrix (or incidence matrix) for a simple graph with n vertices is an n x n matrix with entries that are all 0 or 1. The entries on the diagonal are all 0, and the entry in the ih row and jth column is 1 if there is an edge between vertex i and vertex j and is 0 if there is not an edge between vertex...
Using Java Code. Thankyou. The file has 1 edge per line in the format (origin,destination) - Assume the graph is not directional - (2,5) means there is a path from 2 to 5 and from 5-2 For the Lab write the following code. 1) Write a function to find all adjacent vertices to a node N ( find_adjacent(n)) 2) Write a function that will return true or false that an edge exists ( edge_exists(origin,destination)) 3) Print out all Vertices using...