Edge List
Last updated
import java.util.*;
class Edge {
int src, dest, weight;
// Constructor for an unweighted edge
public Edge(int src, int dest) {
this.src = src;
this.dest = dest;
this.weight = 1; // Default weight (for unweighted graphs)
}
// Constructor for a weighted edge
public Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
}
}
class Graph {
private List<Edge> edgeList; // List of edges
private int vertices; // Number of vertices
public Graph(int vertices) {
this.vertices = vertices;
this.edgeList = new ArrayList<>();
}
// Method to add an unweighted edge
public void addEdge(int src, int dest) {
edgeList.add(new Edge(src, dest));
}
// Method to add a weighted edge
public void addEdge(int src, int dest, int weight) {
edgeList.add(new Edge(src, dest, weight));
}
// Method to remove an edge
public void removeEdge(int src, int dest) {
edgeList.removeIf(edge -> edge.src == src && edge.dest == dest);
}
// Method to check if an edge exists
public boolean hasEdge(int src, int dest) {
for (Edge edge : edgeList) {
if (edge.src == src && edge.dest == dest) {
return true;
}
}
return false;
}
// Method to get all neighbors of a vertex
public List<Integer> getNeighbors(int vertex) {
List<Integer> neighbors = new ArrayList<>();
for (Edge edge : edgeList) {
if (edge.src == vertex) {
neighbors.add(edge.dest);
}
}
return neighbors;
}
// Method to print the graph
public void printGraph() {
System.out.println("Graph (Edge List Representation):");
for (Edge edge : edgeList) {
System.out.println(edge.src + " --(" + edge.weight + ")--> " + edge.dest);
}
}
public static void main(String[] args) {
Graph graph = new Graph(5);
// Add unweighted edges
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
// Add weighted edges
graph.addEdge(1, 4, 10);
graph.addEdge(2, 4, 15);
// Print the graph
graph.printGraph();
// Check if an edge exists
System.out.println("Edge (1 -> 4) exists? " + graph.hasEdge(1, 4));
// Remove an edge and print the graph again
graph.removeEdge(1, 4);
System.out.println("After removing edge (1 -> 4):");
graph.printGraph();
// Get neighbors of vertex 3
System.out.println("Neighbors of vertex 3: " + graph.getNeighbors(3));
}
}Graph (Edge List Representation):
0 --(1)--> 1
0 --(1)--> 2
1 --(1)--> 3
2 --(1)--> 3
3 --(1)--> 4
1 --(10)--> 4
2 --(15)--> 4
Edge (1 -> 4) exists? true
After removing edge (1 -> 4):
Graph (Edge List Representation):
0 --(1)--> 1
0 --(1)--> 2
1 --(1)--> 3
2 --(1)--> 3
3 --(1)--> 4
2 --(15)--> 4
Neighbors of vertex 3: [4]
import java.util.*;
class Edge {
int src, dest, weight;
// Constructor for a weighted edge
public Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
}
}
class Graph {
private List<Edge> edgeList; // List of edges
private int vertices; // Number of vertices
public Graph(int vertices) {
this.vertices = vertices;
this.edgeList = new ArrayList<>();
}
// Method to add a weighted edge
public void addEdge(int src, int dest, int weight) {
edgeList.add(new Edge(src, dest, weight));
}
// Method to remove an edge
public void removeEdge(int src, int dest) {
edgeList.removeIf(edge -> edge.src == src && edge.dest == dest);
}
// Method to check if an edge exists
public boolean hasEdge(int src, int dest) {
for (Edge edge : edgeList) {
if (edge.src == src && edge.dest == dest) {
return true;
}
}
return false;
}
// Method to get the weight of an edge
public int getEdgeWeight(int src, int dest) {
for (Edge edge : edgeList) {
if (edge.src == src && edge.dest == dest) {
return edge.weight;
}
}
return -1; // Return -1 if the edge doesn't exist
}
// Method to get all neighbors of a vertex
public List<Integer> getNeighbors(int vertex) {
List<Integer> neighbors = new ArrayList<>();
for (Edge edge : edgeList) {
if (edge.src == vertex) {
neighbors.add(edge.dest);
}
}
return neighbors;
}
// Method to print the graph
public void printGraph() {
System.out.println("Graph (Weighted Edge List Representation):");
for (Edge edge : edgeList) {
System.out.println(edge.src + " --(" + edge.weight + ")--> " + edge.dest);
}
}
public static void main(String[] args) {
Graph graph = new Graph(5);
// Add weighted edges
graph.addEdge(0, 1, 5);
graph.addEdge(0, 2, 10);
graph.addEdge(1, 3, 2);
graph.addEdge(2, 3, 4);
graph.addEdge(3, 4, 7);
// Print the graph
graph.printGraph();
// Check if an edge exists
System.out.println("Edge (1 -> 3) exists? " + graph.hasEdge(1, 3));
// Get the weight of an edge
System.out.println("Weight of edge (2 -> 3): " + graph.getEdgeWeight(2, 3));
// Remove an edge and print the graph again
graph.removeEdge(2, 3);
System.out.println("After removing edge (2 -> 3):");
graph.printGraph();
// Get neighbors of vertex 3
System.out.println("Neighbors of vertex 3: " + graph.getNeighbors(3));
}
}Graph (Weighted Edge List Representation):
0 --(5)--> 1
0 --(10)--> 2
1 --(2)--> 3
2 --(4)--> 3
3 --(7)--> 4
Edge (1 -> 3) exists? true
Weight of edge (2 -> 3): 4
After removing edge (2 -> 3):
Graph (Weighted Edge List Representation):
0 --(5)--> 1
0 --(10)--> 2
1 --(2)--> 3
3 --(7)--> 4
Neighbors of vertex 3: [4]