Le graphique est une vérification d'arbre

--J'ai écrit un code pour vérifier si le graphe donné comme liste d'adjacence dans le fichier d'entrée est un arbre ou non et le sortir dans le fichier de sortie.

- Les commentaires expliquent l'opération qui se passe à chaque étape.

--adjacencyList.get (no_of_vertices) = new ArrayList (); ligne est en train de donner une erreur de compilation

The left-hand side of an assignment must be a variable

- Peut-on faire remarquer au code.

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
import java.util.Iterator;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;

public class isTree implements Comparator {

    /* Variables used ::
       (1) adjacencyList  -- Contains the adjacency list as an array after being extracted from list. Each row contains all vertices connected 
                            to the corresponding vertex given by row.
       (2) no_of_edges    -- Contains number of edges in the graph
       (3) no_of_vertices -- Contains number of vertices in graph
       (4) result         -- Contains whether a graph is a tree or not
     */
    ArrayList> adjacencyList = new ArrayList>(100);
    int no_of_edges=0;
    int no_of_vertices=0;
    String result;

    /* Operation control function which calls all other functions on the graph input provided. */
    private isTree(String input_file,String output_file){
        createArrayList(input_file);    
        no_of_edges = findingEdges(adjacencyList);
        if(!result.equals("Not a Tree")){
            if(checkCondition1(no_of_edges,no_of_vertices) && checkCondition2(adjacencyList,no_of_vertices))
                result = "Its a Tree";
            else
                result = "Not a Tree";
        }
       printingResultInFile(output_file); 
    }

    /* Creation of ArrayList from the input File by extracting line by line and using comma delimiter to split the numbers in each line
       which are then stored as vertices in increasing order in the adjacency row of the list. */
    private void createArrayList(String input_name){
        int j;
        String line;
        String [] vertices;
        Scanner in=null;
        int len;
        try {
            in = new Scanner(new File(input_name));
        }catch(FileNotFoundException e) {
            System.out.println("File Not Found");
        }
        while(in.hasNextLine()){
            List rowList = adjacencyList.get(no_of_vertices);
        rowList = new ArrayList();
        line = in.nextLine();
        if(line.equalsIgnoreCase("null")){
            no_of_vertices++;
            continue;
        }
        vertices = line.split(",");
        len = vertices.length;
        for(j=0;j= b)
               return 1;
           else 
               return -1;
    }

    /* Finding number of edges by calculating the total number of vertices in all the rows of adjacency list and dividing by 2 since edge 
       edge relation is symmetric. */

    private int findingEdges(ArrayList> adjacencyList){
        int i;
        for(i=0;i> adjacencyList,int no_of_vertices){
        pArrayStackInt stack = new pArrayStackInt(no_of_vertices);
        int j=1,k;
        ArrayList template = new ArrayList();
        stack.push(1);
        while(!stack.isEmpty()){
            j=stack.pop();
            template.add(j);
            Iterator itr = adjacencyList.get(j-1).iterator();
            j++;
            while(itr.hasNext()){
                k=itr.next();
                if(!template.contains(k))
                    stack.push(k);
            }

        }
        if(template.size()==no_of_vertices)
            return true;
        else
            return false;
    }

    /* Printing whether the given input file is a tree or not onto the output file depending upon the value stored
       in result variable */

    private void printingResultInFile(String output_file){
        PrintWriter out=null;
        try {
            out = new PrintWriter(output_file);
        }
        catch (FileNotFoundException e) {
            System.out.println("File Not Found");
        }
        out.write(result);
        out.close();
    }

    /* 2 inputs are provided at the runtime as arguments firstly input_file name and secondly output file name.
       Input file name is stored in args[0] and Output file name is stored in args[1]. With these input file name
       and output file name a new instance of isTree is created */

    public static void main(String[] args){
        String input_file = args[0];
        String output_file = args[1];
        new isTree(input_file,output_file);
    }
}
 /* Implementing stack data structure by providing push, pop and isEmpty operations */
 class pArrayStackInt{
    private int head[];
    private int pointer;

    public pArrayStackInt(int capacity){
        head = new int[capacity];
        pointer = -1;
    }
    public boolean isEmpty(){
        return pointer == -1;
    }
    public void push(int i){
        if(pointer+1 < head.length)
            head[++pointer] = i;
    }
    public int pop(){
        return head[pointer--];
   }
 }
0
ajouté édité
Vues: 1

1 Réponses

It should be List someName = adjacencyList.get(no_of_vertices); The .get() returns an ArrayList which you must save off to another variable to use it later.

0
ajouté
Maintenant, c'est un autre problème complètement. Vous essayez d'atteindre un emplacement sur le tableau supérieur à la longueur du tableau.
ajouté l'auteur noMAD, source
J'ai essayé votre suggestion et ai passé des fichiers d'entrée et de sortie comme entrées de ligne de commande mais il me donne cette erreur :: Exception dans le fil "principal" java.lang.IndexOutOfBoundsException: Index: 0, taille: 0 à java.util.ArrayList. rangeCheck (source inconnue) à java.util.ArrayList.get (source inconnue) à isTree.createArrayList (isTree.java:57) à isTree. (isTree.java:32) à isTree.main (isTree.java: 161)
ajouté l'auteur tcp, source