Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Basic Implementation Of Graph #85

Merged
merged 11 commits into from
Oct 5, 2023
Binary file added Graphs/Graph.class
Binary file not shown.
Binary file added Graphs/ImplementingGraphs.class
Binary file not shown.
49 changes: 49 additions & 0 deletions Graphs/ImplementingGraphs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.util.*;


class Graph {
private int V;
private Map<Integer, List<Integer>> adjacencyList;

// Constructor
public Graph(int V) {
this.V = V;
this.adjacencyList = new HashMap<>();
for (int i = 0; i < V; i++) {
adjacencyList.put(i, new ArrayList<>());
}
}
public void addEdge(int v, int w) {
adjacencyList.get(v).add(w);
adjacencyList.get(w).add(v);
}
// Print the graph
public void printGraph() {
for (Map.Entry<Integer, List<Integer>> entry : adjacencyList.entrySet()) {
int vertex = entry.getKey();
List<Integer> neighbors = entry.getValue();
System.out.print("Vertex " + vertex + " is connected to: ");
for (Integer neighbor : neighbors) {
System.out.print(neighbor + " ");
}
System.out.println();
}
}
}

public class ImplementingGraphs {
public static void main(String[] args) {

Graph graph = new Graph(5);

graph.addEdge(0, 1);
graph.addEdge(0, 4);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 3);
graph.addEdge(3, 4);

graph.printGraph();
}
}
1 change: 1 addition & 0 deletions Graphs/tempCodeRunnerFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ingGraphs