Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: In degree and out degree of a graph

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

    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");
    	 		    }       
    	   }
         }
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,286
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: In degree and out degree of a graph

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Conversion of the temperature degree
    By alialomran in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 4th, 2013, 09:16 AM
  2. Replies: 1
    Last Post: January 10th, 2013, 11:45 PM
  3. I want a much programming degree.
    By Mar5000k in forum The Cafe
    Replies: 0
    Last Post: January 6th, 2013, 09:43 AM
  4. Replies: 1
    Last Post: February 11th, 2012, 07:38 AM
  5. Java Bar graph takes user input to create graph?
    By kenjiro310 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2011, 08:37 AM