Question
1. Your program will take a file name as an argument. The first two lines of the file will contain the number of persons P
1. Your program will take a file name as an argument. The first two lines of the file will contain the number of persons P and number of knows relationships R
in the file, respectively. The next P lines each start with a consecutive integer (1..P) followed by a space, followed by the persons name, which is a string of lowercase characters (a-z).
2. Your program will represent the information from the file as a graph using either an adjacent list or adjacent matrix representation.
3. Your program will compute and output several statistics about the graph. A sample output is attached below.
Number of vertices
Number of edges
Minimum degree of a vertex
the degree of a vertex is the number of edges
connected to the vertex, i.e., the number of people that person k
nows.
Maximum degree of a vertex
Average degree over all vertices, i.e., the average number of people each person
knows.
Average shortest path length between pairs of vertices
Longest shortest path between two vertices
Size and members of the largest complete subgraph (clique) in the social network, i.e., largest group of people where everybody knows everybody. If more than one
largest group occurs, then output any one of them.
MY CODE: Next step??
public class graphs {
public static String IN[] = new String[22]; static int vertex_nr;
final static int inf = 10000; // infinite static int adjM[][] = { {0, 1, inf, inf, inf, inf, inf, inf}, {1, 0, 1, inf, inf, 1, 1, inf}, {inf, 1, 0, 1, inf, 1, 1, inf}, {inf, inf, 1, 0, 1, inf, inf, inf}, {inf, inf, inf, 1, 0, inf, inf, inf}, {inf, 1, 1, inf, inf, 0, 1, 1}, {inf, 1, 1, inf, inf, 1, 0, 1}, {inf, inf, inf, inf, inf, 1, 1, 0} };
static boolean visit[] = {false, false, false, false, false, false, false, false};
static int dist[] = {inf, inf, inf, inf, inf, inf, inf, inf};
static int route[] = new int[8];
static int shortest(){ int min = inf; int position = 0; for(int i=0; i<8; i++){ if(dist[i] min = dist[i]; position = i; }//fi }//for(i) return position; }//sh
static void dijkstra(int start){ for(int i=0; i<8; i++){ dist[i] = adjM[start][i]; }//for(i) visit[start] = true; dist[start] = 0; for(int i=0; i<6; i++){ int current = shortest(); visit[current] = true; for(int j=0; j<8; j++){ if(!visit[j]){ if(dist[current]+adjM[current][j] dist[j] = dist[current] + adjM[current][j]; route[j] = current; }//fi }//fi }//for(j)
}//for(i)
}//dijk
static void routing(int start, int end){ int tmp = route[end]; int find[] = new int[8]; int cnt = 1;
find[0] = end; find[cnt] = tmp;
while(tmp != start){ find[++cnt] = route[tmp]; tmp = route[tmp]; }//while System.out.print("shortest path from "+vertex_nr+" to "+end+": "); for(int i=cnt; i>0; i--){ System.out.print("("+vertex_nr+" -> "+find[i-1]+")"); }//for(i) System.out.println(""); }//routing public static void main(String[] args) { String IN[] = new String[22]; try{ //start template 3 int cnt = 0; BufferedReader r = new BufferedReader(new FileReader("file.txt")); while(true){ String line = r.readLine(); if(line == null){ break; } //fi IN[cnt] = line;
System.out.println(IN[cnt]); cnt++; // } } catch(IOException ioe) { System.err.println("Input Output Exception"); } //end template 3 String personNum = IN[0]; String knowsNum = IN[1]; //System.out.println(personNum); // System.out.println(knowsNum); // create the graph int V = 9; Graph graph = new Graph(V); addEdge(graph, 1, 2); addEdge(graph, 2, 3); addEdge(graph, 3, 4); addEdge(graph, 4, 5); addEdge(graph, 2, 6); addEdge(graph, 2, 7); addEdge(graph, 3, 6); addEdge(graph, 3, 7); addEdge(graph, 6, 7); addEdge(graph, 6, 8); addEdge(graph, 7, 8);
// print the adjacency list representation of // the above graph printGraph(graph); System.out.println(" Dijkstra Algorithm: "); /*vertex_nr=0: bob; vertex_nr=1: tom; vertex_nr=2: george; vertex_nr=3: john; vertex_nr=4: mary; vertex_nr=5: sally; vertex_nr=6: jane; vertex_nr=7: allice;*/ //vertex_nr = 0; // change vertex_nr=0..6 for target person at a time dijkstra(vertex_nr); for(int i=vertex_nr; i<8; i++){ routing(0, i); }//for(i) System.out.println(""); }//main static class Graph { int V; LinkedList adjListArray[]; // constructor Graph(int V) { this.V = V; // define the size of array as // number of vertices adjListArray = new LinkedList[V]; // Create a new list for each vertex // such that adjacent nodes can be stored for(int i = 0; i < V ; i++){ adjListArray[i] = new LinkedList<>(); } } } // Adds an edge to an undirected graph static void addEdge(Graph graph, int src, int dest) { // Add an edge from src to dest. graph.adjListArray[src].add(dest); // Since graph is undirected, add an edge from dest // to src also graph.adjListArray[dest].add(src); } // A utility function to print the adjacency list // representation of graph static void printGraph(Graph graph) { for(int v = 1; v < graph.V; v++) { System.out.println("Adjacency list of vertex "+ v); System.out.print("head"); for(Integer pCrawl: graph.adjListArray[v]){ System.out.print(" -> "+pCrawl); } System.out.println(" "); } } }//Main
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started