package algorithms;
import java.util.Vector;

/* A GraphNode has a stored value (type String), a reference to a list of 
 * adjacent nodes, and a "count" variable to help with implementing graph search.
 */

public class GraphNode {
	
	// data members
	String value;	  // string info stored at node
	Vector next;      // vector of references to adjacent nodes
	int count;		  // can be used to implement graph searching
	
	// constructor for a node with a given "count" value
	// string is set to "", and there are no adjacent nodes
	public GraphNode(int count)
	{
		value = "";
		next = new Vector();
		this.count = count;
	}
	
	// no-arg constructor
	// string is set to "", count to 0, and there are no adjacent nodes
	public GraphNode()
	{
		this(0); // call to 1-arg constructor
	}
	
	// accessor for value
	public String getValue()
	{
		return value;
	}
	
	// accessor for next
	public Vector getNext()
	{
		return next;
	}
	
	// accessor for count
	public int getCount()
	{
		return count;
	}
	
	// mutator for value
	public void setValue(String v)
	{
		value = v;
	}
	
	// returns the degree of this node
	public int degree()
	{
		if(next == null)
			return 0;
		else
			return next.size();
	}
	
	// method to check if an edge exists
	public boolean isAdjacent(GraphNode h)
	{
		if(this.next == null)
			return false;
		for(int i=0; i<this.next.size(); i++)
			if(this.next.elementAt(i) == h)
				return true;
		return false;
	}
	
	// method to add an edge from "this" to h
	public void addEdge(GraphNode h)
	{
		if(this.isAdjacent(h))
			return;
		// in this.next, add a reference to h
		this.next.add(h);
		
		// in h.next, add a reference to this
		h.next.add(this);
	}
	
	// method to return node info in a string, ready for printing
	// the string tells the value of the node and the values of the 
	// adjacent nodes
	public String toString()
	{
		StringBuffer buff = new StringBuffer();
		buff.append(value + " is adjacent to: ");
		if(degree()==0)
			buff.append("<none>");
		else
			// get info about all the nodes adjacent to this node
		{	int i;
			for(i=0; i<degree()-1; i++)
			{
				GraphNode h = (GraphNode)this.next.elementAt(i);
				buff.append(h.value + ", ");
			}
			// here's the last node in the adjacency list
			GraphNode h = (GraphNode)this.next.elementAt(i);
			buff.append(h.value);
		}
		return buff.toString();
	}

}