package edu.cmu.cs.l3.graph;

import java.util.*;

public abstract class UndirectedGraph extends Graph {

    public UndirectedGraph() {
    }

    public void createEdge(Vertex u, Vertex v) {

        List ulist = (List)m_adjlists.get(u.getID());
        List vlist = (List)m_adjlists.get(v.getID());

        if (!vlist.contains(u)) {
            vlist.add(u); ulist.add(v);
        }
    }

    public void removeVertex(Vertex v) {
        List adjs;
        int i;

        adjs = getAdjacents(v);
        for (i = 0; i < adjs.size(); i++) {
            Vertex u = (Vertex) adjs.get(i);
            removeEdge(u,v);
        }

        v.deadMark();
    }

    public void removeEdge(Vertex u, Vertex v) {
        ((List)m_adjlists.get(v.getID())).remove(u);
        ((List)m_adjlists.get(u.getID())).remove(v);
    }

    public int getDegree(Vertex v, int type) throws GraphException {
        return ((List)m_adjlists.get(v.getID())).size();
    }

    public boolean isAdjacent(Vertex v, Vertex u) {
        return ((List)m_adjlists.get(u.getID())).contains(v);
    }

    public List getAdjacents(Vertex v) {
        List adjs = new ArrayList();
        adjs.addAll((List)m_adjlists.get(v.getID()));
        return adjs;
    }

}