Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
bin
classes
85 changes: 43 additions & 42 deletions src/main/java/assembly/Graph.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,60 @@

/**
* @author dev
*
* @param <E>
*/
public class Graph<E> {

/**
* ArrayList to hold edges for random access
*/
private ArrayList<Edge<E>> edges;
/**
* ArrayList to hold edges for random access
*/
private ArrayList<Edge<E>> edges;

public Graph() {
edges = new ArrayList<Edge<E>>();
}
public Graph() {
edges = new ArrayList<Edge<E>>();
}

/**
* @param vertex
* @return - boolean
*
* Add Edge to the List
*/
public boolean addEdge(Edge<E> vertex) {
if (edges.contains(vertex))
return false;
edges.add(vertex);
return true;
}
/**
* @param vertex
* @return - boolean Add Edge to the List
*/
public boolean addEdge(Edge<E> vertex) {
if (edges.contains(vertex))
return false;
edges.add(vertex);
return true;
}

public boolean contains(Edge<E> vertex) {
return edges.contains(vertex);
}
public boolean contains(Edge<E> vertex) {
return edges.contains(vertex);
}

public Edge<E> get(int index) {
return edges.get(index);
}
public Edge<E> get(int index) {
return edges.get(index);
}

/**
* @return : number of Edges in Graph
*/
public int count() {
return edges.size();
}
/**
* @return : number of Edges in Graph
*/
public int count() {
return edges.size();
}

/**
* Checks if two graphs are equal. Store all of Edges of larger Graph & Graphs are equal only if
* temp is unchanged *
*/
public boolean equals(Graph<E> other) {
/**
* Checks if two graphs are equal. Store all of Edges of larger Graph & Graphs are equal only if temp is unchanged *
*/
public boolean equals(Graph<E> other) {

if (other.edges.size() != edges.size())
return false;
if (other.edges.size() != edges.size())
return false;

ArrayList<Edge<E>> temp = new ArrayList<Edge<E>>(other.edges);
ArrayList<Edge<E>> temp = new ArrayList<Edge<E>>(other.edges);

return temp.retainAll(edges);
}
return temp.retainAll(edges);
}

public void reset() {
for (int i = 0; i < count(); i++)
get(i).setDistance(Integer.MAX_VALUE);
}
}
140 changes: 38 additions & 102 deletions src/main/java/graph/Edge.java
Original file line number Diff line number Diff line change
@@ -1,112 +1,48 @@
package graph;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;



/**
* @author dev
*
* @param <E>
*
* Base class for creating an edge.Stores the number of edges,an Element <E> - used in
* weighted graphs and game theory in-case of incoming gains,this is a provision for the
* change in fuel prices or new routes. It has individual edge identifier,weight and pointers
* for reference to other edges.
* Base class for creating an edge.Stores the number of edges,an Element <E> - used in weighted graphs and game theory
* in-case of incoming gains,this is a provision for the change in fuel prices or new routes. It has individual edge
* identifier,weight and pointers for reference to other edges.
*/
public class Edge<E> {

private static int ID = 0;

private E element;

private int id;

private int weight;

private LinkedList<Link<E>> pointers;

public Edge() {
this(null, Integer.MAX_VALUE);
}

public Edge(E elem, int distance) {
this.element = elem;
id = ID++;
pointers = new LinkedList<Link<E>>();
this.weight = distance;
}

public int getId() {
return id;
}

public E getElem() {
return element;
}

public void setElem(E elem) {
this.element = elem;
}

public int getDistance() {
return weight;
}

public void setDistance(int dist) {
weight = dist;
}

// add a connection
public void connectTo(Edge<E> other) {
Link<E> c = new Link<E>(this, other);

// check for duplicates
if (!pointers.contains(c))
pointers.add(c);

// reference Connector in other Edge as well
LinkedList<Link<E>> conn = other.getConnections();
if (!conn.contains(c))
conn.add(c);
}

public void connectTo(Edge<E> other, int distance) {
Link<E> c = new Link<E>(this, other, distance);
if (!pointers.contains(c))
pointers.add(c);
}

public LinkedList<Link<E>> getConnections() {
return pointers;
}

public void sortConnections() {
Collections.sort(pointers);
}

public Iterator<Link<E>> iterator() {
return pointers.iterator();
}

// one Edge is equal to another if the two elems are equal to each other
// and they have the same Connections
public boolean equals(Edge<E> other) {

if (other.pointers.size() != pointers.size())
return false;

LinkedList<Link<E>> temp = new LinkedList<Link<E>>(pointers);

// edges are order agnostic
// if the elements are equal and the Lists are equal, regardless of order
// then the Edges are equal
return element.equals(other.getElem()) && temp.retainAll(other.pointers);
}

public String toString() {
return this.element.toString();
}
private static int ID = 0;
private E element;
private int id;
private int weight;
private LinkedList<Link<E>> pointers;

public Edge(E elem, int distance) {
this.element = elem;
id = ID++;
pointers = new LinkedList<Link<E>>();
this.weight = distance;
}

public int getDistance() {
return weight;
}

public void setDistance(int dist) {
weight = dist;
}

public void connectTo(Edge<E> other, int distance) {
Link<E> c = new Link<E>(this, other, distance);
if (!pointers.contains(c))
pointers.add(c);
}

public LinkedList<Link<E>> getConnections() {
return pointers;
}

@Override
public String toString() {
return this.element.toString();
}
}
83 changes: 33 additions & 50 deletions src/main/java/graph/Link.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,37 @@
package graph;

public class Link<E> implements Comparable<Link<E>> {

private Edge<E> source, sink;
private int distance;

/*
* constructor- creates a Link object to Connect two Nodes together with a standard distance of 0
* , it is assumed That distance is either weighted through the Edges or otherwise is irrelevant
*/
public Link(Edge<E> source, Edge<E> sink) {
this(source, sink, 0);
}

public Link(Edge<E> source, Edge<E> sink, int distance) {
this.source = source;
this.sink = sink;
this.distance = distance;
}

public Edge<E> getSource() {
return source;
}

public Edge<E> getSink() {
return sink;
}

public int getDistance() {
return distance;
}

public void setDistance(int distance) {
this.distance = distance;
}

/*
* Two connectors are equal if the two Edges are equal and the distance is equal
*/
public boolean equals(Link<E> other) {
return source.equals(other.getSource()) && sink.equals(other.getSink())
&& distance == other.getDistance();
}

@Override
public String toString() {
return "Link [source=" + source + ", sink=" + sink + ", distance=" + distance + "]";
}

public int compareTo(Link<E> other) {
return this.distance - other.distance;
}
private Edge<E> source, sink;
private int distance;

public Link(Edge<E> source, Edge<E> sink, int distance) {
this.source = source;
this.sink = sink;
this.distance = distance;
}

public Edge<E> getSource() {
return source;
}

public Edge<E> getSink() {
return sink;
}

public int getDistance() {
return distance;
}

public void setDistance(int distance) {
this.distance = distance;
}

@Override
public String toString() {
return "Link [source=" + source + ", sink=" + sink + ", distance=" + distance + "]";
}

public int compareTo(Link<E> other) {
return this.distance - other.distance;
}
}
Loading