
// LoadAdjMatrix.java
// Dimitrios Michail and Contributors, 2021
// Modified by Andrew Davison, ad@coe.psu.ac.th, Jan. 2023

/* Read data from a text file, build the graph, display it, 
   then save it to a 'Copy' file, which is then read back in
   and redisplayed.

   There are two method to test the unweighted and weighted 
   read methods: JGTUtils.readAdjMatrix() and 
   JGTUtils.readWAdjMatrix().

   The unweighted file is mat1.txt; 
   the weighted file is mat2.txt (both are directed graphs)

   The weighted graph uses my WeightedEdge class for the edges
   which assumes the weights are integers.

   For related code, see LoadAdjList.java, and LoadGraphML.java


  Usage:
    > compile LoadAdjMatrix.java
    > run LoadAdjMatrix
*/

import java.io.*;
import java.util.*;

import org.jgrapht.*;
import org.jgrapht.graph.*;



public class LoadAdjMatrix
{

  public static void main(String[] args)
  {
    unweighted();
    weighted();
  }  // end of main()



  private static void unweighted()
  // read, display, save, read back in, display
  {
    System.out.println("----------- unweighted -----------------\n");

    // read and build
    String input = JGTUtils.loadText("mat1.txt");
    Graph<String, DefaultEdge> g = 
                JGTUtils.readAdjMatrix(input, true);  
                               // the graph is directed
    // display
    System.out.println("\nMatrix 1 graph:\n" + g);
    // JGTUtils.displayAsDOT(g);
    JGTUtils.displayAsMX("unweighted", g);

    // save
    String gStr = JGTUtils.writeAdjMatrix(g);
    System.out.println("\nAdj Matrix version:\n" + gStr);
    JGTUtils.saveText("mat1Copy.txt", gStr);

    // read in again
    System.out.println();
    input = JGTUtils.loadText("mat1Copy.txt");
    g = JGTUtils.readAdjMatrix(input, true);

    System.out.println("\nMatrix 1 Copy graph:\n" + g);
    JGTUtils.displayAsMX("unweighted copy", g);
  }  // end of unweighted()



  private static void weighted()
  // read, display, save, read back in, display
  {
    System.out.println("\n----------- weighted -----------------\n");

    // read and build
    String input = JGTUtils.loadText("mat2.txt");
    Graph<String, WeightedEdge> g = 
            JGTUtils.readWAdjMatrix(input, true);  
                               // the graph is directed
    // display
    System.out.println("\nMatrix 2 graph:\n" + g);
    System.out.println("W C->D: " + g.getEdgeWeight(g.getEdge("C", "D")));
    // JGTUtils.displayAsDOT(g);
    JGTUtils.displayAsMX("weighted", g);

    // save
    String gStr = JGTUtils.writeAdjMatrix(g);
    System.out.println("\nAdj Matrix version:\n" + gStr);
    JGTUtils.saveText("mat2Copy.txt", gStr);

    // read in again
    System.out.println();
    input = JGTUtils.loadText("mat2Copy.txt");
    g = JGTUtils.readWAdjMatrix(input, true);

    System.out.println("\nMatrix 2 copy graph:\n" + g);
    JGTUtils.displayAsMX("weighted copy", g);

    System.out.println("W C->D: " + g.getEdgeWeight(g.getEdge("C", "D")));
  }  // end of weighted()


}  // end of LoadAdjMatrix class
