# In degree and out degree of a graph

• May 2nd, 2014, 09:17 PM
custurd122000
In degree and out degree of a graph
I have this code for reading a text file and printing out information about the graph, the program works perfectly but I need to figure out the in degree and out degree of the graph if it is an directed graph. So my question is how would I go about implementing that? I know what in degree and out degree does but in terms of the code I'm not so sure, so I'm thinking if the case of vertex 0,1 the first vertex comes before the second vertex so that would in an in degree right? But how would I do that?

graphs.txt
6,1
0,2 0,4 1,4 1,5 2,1 2,3 2,5 3,2 3,4 4,1 4,5 5,1 5,3 5,2

Code Java:

```import java.io.File; import java.util.Scanner;     public class Vertices {   public static void main(String[] args) throws Exception { @SuppressWarnings("resource") Scanner inFile = new Scanner(new File("graphs.txt")); // Read the number of vertices String line = inFile.nextLine(); String[] data=line.split("[\\,]"); int order=Integer.parseInt(data[0]); int typeOfGraph=Integer.parseInt(data[1]);   //Prints results of the order System.out.println("The order of the graph = " + order);     while(inFile.hasNext()) { line=inFile.nextLine(); int index =0; int count=0; char edges = ','; while(index<line.length()) {   if(line.charAt(index)==edges){ count++; } index++; } /* PRINTS the result for each line!!! */ System.out.println("The size of graph = "+count);     //Determines if graph is zero or one   String[] data2=line.split("[\\:]"); String[][] vertices = new String[ data2.length ][]; for (int i = 0; i < data2.length; i++){ vertices[i] = data2[i].split("[ ]"); } count=0; index=0; System.out.println("Graph: "); for (int j = 0; j < vertices.length; j++){ for (int i = 0; i < vertices[j].length; i++){   System.out.print("Index "+i+": "+vertices[j][i]+" \n"); } if(typeOfGraph==0) { System.out.println("The graph is an undirected graph"); } if( typeOfGraph==1) {   System.out.println("The graph is a directed graph"); } } } } }```
• May 2nd, 2014, 09:20 PM
Norm
Re: In degree and out degree of a graph
Quote:

I need to figure out the in degree and out degree of the graph if it is an directed graph
That sounds like you need an algorithm BEFORE you try writing any code. None of what is in that quote has anything to do with writing a program in java.
If you have an algorithm you are writing code for, post it and any specific questions or problems you are having writing java code.