0

I am fairly new to java and I have been struggling with this exercise for two weeks now(It's an homework exercise in my school). I need to create a topological sort and print out all of the possible connections. I have read a lot about topological sorting now, but we have this certain line of code that we have to work with. I'm pretty sure I could do the topological sorting when I have the list of vertices. My problem is, I don't know how to list all of the vertices from this given code. Could anyone give me some tips or leads or perhaps an example, I would really really appreciate it.

Here is the given code we need to work with:

import java.util.*;

public class Answer {

   public static void main (String[] args) {
      Answer a = new Answer();
      a.run();
   }

   public void run() {

      // TODO!!! YOUR TESTS HERE!

      Graph g = new Graph ("G");
      Vertex a = new Vertex ("A");
      Vertex b = new Vertex ("B");
      Vertex c = new Vertex ("C");
      g.first = a;
      a.next = b;
      b.next = c;
      Edge ab = new Edge ("AB");
      Edge ac = new Edge ("AC");
      Edge ba = new Edge ("BA");
      Edge ca = new Edge ("CA");
      a.first = ab;
      b.first = ba;
      c.first = ca;
      ab.next = ac;
      ab.target = b;
      ac.target = c;
      ba.target = a;
      ca.target = a;
      System.out.println (g);
   }


   class Vertex {

      String id;
      Vertex next;
      Edge first;

      Vertex (String s, Vertex v, Edge e) {
         id = s;
         next = v;
         first = e;
      } 

      Vertex (String s) {
         this (s, null, null);
      }

      @Override
      public String toString() {
         return id;
      }

      // TODO!!! Your Vertex methods here!

   } // Vertex


   class Edge {

      String id;
      Vertex target;
      Edge next;

      Edge (String s, Vertex v, Edge e) {
         id = s;
         target = v;
         next = e;
      }

      Edge (String s) {
         this (s, null, null);
      }

      @Override
      public String toString() {
         return id;
      }

      // TODO!!! Your Edge methods here!

   } // Edge


   class Graph {

      String id;
      Vertex first;

      Graph (String s, Vertex v) {
         id = s;
         first = v;
      }

      Graph (String s) {
         this (s, null);
      }

      @Override
      public String toString() {
         String nl = System.getProperty ("line.separator");
         StringBuffer sb = new StringBuffer (nl);
         sb.append (id + nl);
         Vertex v = first;
         while (v != null) {
            sb.append (v.toString() + " --> ");
            Edge e = v.first;
            while (e != null) {
               sb.append (e.toString());
               sb.append ("(" + v.toString() + "->" 
                  + e.target.toString() + ") ");
               e = e.next;
            }
            sb.append (nl);
            v = v.next;
         }
         return sb.toString();
      }

      // TODO!!! Your Graph methods here!

   } // Graph
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Renet
  • 339
  • 6
  • 13
  • 1
    hint: where it says // TODO!!! ... you fill in code to do the things you need to do. in the run() method, you can call the new functions to get your answers. – Randy Nov 29 '12 at 13:49

2 Answers2

2

Apparently, the graph has a reference to the first vertex, and the vertices themselves are linked together into a singly linked list. This code should be all you need to collect the vertices into a Java list:

public List<Vertex> allVertices(Graph g) {    
  final List<Vertex> vertices = new ArrayList<>();
  for (Vertex v = g.first; v != null; v = v.next)
     vertices.add(v);
  return vertices;
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

I would suggest that you add a "lastvisited" integer field to the edge which is set to zero, or use a boolean "visited"(true/false). Then start at one vertex. Assuming the graph is connected, you will reach all vertexes by going over the unvisited edges for one vertex, then following the edges to the vertex it leads to, marking the edge as followed, and calling your count function for this vertex recursively.

I.E.: count(node) = sum(my unvisited edges) mark_edge_as_visited(edge), count(edge.target)

Please note that you also have to consider that the graph appears to be a directed graph, so an edge leading from a to b and from b to a is counted as two edges.

Edit: I made a mistake, you also need to mark the vertex as visited, or it will be visited twice (I was thinking of an undirected graph).

Adder
  • 5,708
  • 1
  • 28
  • 56