Quick actions

cmd+k|ctrl+k

Navigation

Languages

Graph

Snippet info

Language

Java

Visibility

public

Author

ingserge

Created

2024-10-10T00:04:31.793022Z

Updated

2024-10-11T04:36:09.679007Z

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Hashtable;
import java.util.Map.Entry;

class Graph{
    public Map<String,List<String>> adjacentList= new Hashtable<String, List<String>>();
    
    /**
     * Adds a vertex to the graph.
     * Returns true if added successfully or false if not.
     * **/
    public boolean addVertex(String vertex){
        if (vertex ==null || vertex.isEmpty()){
            System.out.println("The vertex cannot be null or empty.");
            return false;
        }
        if (adjacentList.containsKey(vertex)){
            System.out.println("Vertex "+vertex+" already exists.");
            return false;
        }
        adjacentList.put(vertex, new ArrayList<String>());
        return true;
    }
    
    /**
     * Adds an edge between existing verteces.
     * Returns true if added successfully or false if not.
     * **/
    public boolean addEdge(String node, String edge){
        if (node == null || node.isEmpty()){
            System.out.println("The start of the edge cannot be null or empty.");
            return false;
        }
        if (edge == null || edge.isEmpty()){
            System.out.println("The end of the edge cannot be null or empty.");
            return false;
        }
        if (!adjacentList.containsKey(node)){
            System.out.println("The start vertex "+node+" does not exist.");
            return false;
        }
        if (!adjacentList.containsKey(edge)){
            System.out.println("The end vertex "+edge+" does not exist.");
            return false;
        }
        adjacentList.get(node).add(edge);
        adjacentList.get(edge).add(node);
        
        return true;
    }
    
    /**
     * Prints all the connections if not empty.
     **/
    public void showConnections(){
        if (adjacentList== null || adjacentList.isEmpty()){
            System.out.println("The graph is empty");
        }
        for (Map.Entry<String,List<String>> entry  :  adjacentList.entrySet()){
            System.out.print(entry.getKey()+" --> ");
            for (String value: entry.getValue()){
                System.out.print(" "+value);
            }
            System.out.println(" ");
        }
    }
}

class Main {
    public static void main(String[] args) {
        Graph graph = new Graph();
        
        graph.addVertex("0");
        graph.addVertex("1");
        graph.addVertex("2");
        graph.addVertex("3");
        graph.addVertex("4");
        graph.addVertex("5");
        graph.addVertex("6");
        graph.addEdge("3", "1");
        graph.addEdge("3", "4");
        graph.addEdge("4", "2");
        graph.addEdge("4", "5");
        graph.addEdge("1", "2");
        graph.addEdge("1", "0");
        graph.addEdge("0", "2");
        graph.addEdge("6", "5");
        graph.showConnections();
    }
}
INFO