Learn the fundamentals of graph theory and explore graph representations. Understand how graphs can model complex relationships across various domains.
Example of an undirected graph with 6 vertices and 7 edges
import java.util.*;
public class Graph {
private int V; // Number of vertices
private List> adjList; // Adjacency list
// Constructor
public Graph(int vertices) {
this.V = vertices;
// Initialize adjacency list
adjList = new ArrayList<>();
for (int i = 0; i < V; i++) {
adjList.add(new ArrayList<>());
}
}
// Add an edge to the graph
public void addEdge(int source, int destination) {
// For an undirected graph, add edges in both directions
adjList.get(source).add(destination);
adjList.get(destination).add(source);
}
// Add directed edge to the graph
public void addDirectedEdge(int source, int destination) {
adjList.get(source).add(destination);
}
// Get the adjacency list for a specific vertex
public List getNeighbors(int vertex) {
return adjList.get(vertex);
}
// Print the graph representation
public void printGraph() {
for (int i = 0; i < V; i++) {
System.out.print("Vertex " + i + " is connected to: ");
for (Integer neighbor : adjList.get(i)) {
System.out.print(neighbor + " ");
}
System.out.println();
}
}
// Get the number of vertices
public int getVertexCount() {
return V;
}
// Example usage
public static void main(String[] args) {
// Create a graph with 5 vertices
Graph graph = new Graph(5);
// Add edges
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);
// Print the graph
graph.printGraph();
}
}
For additional hands-on practice and exercises on graph theory, check out our comprehensive guided project:
public class GraphMatrix {
private int V; // Number of vertices
private boolean[][] adjMatrix; // Adjacency matrix
// Constructor
public GraphMatrix(int vertices) {
this.V = vertices;
adjMatrix = new boolean[V][V];
}
// Add an edge to the graph
public void addEdge(int source, int destination) {
// For an undirected graph, add edges in both directions
adjMatrix[source][destination] = true;
adjMatrix[destination][source] = true;
}
// Add directed edge to the graph
public void addDirectedEdge(int source, int destination) {
adjMatrix[source][destination] = true;
}
// Check if two vertices are connected
public boolean isEdge(int source, int destination) {
return adjMatrix[source][destination];
}
// Print the graph representation
public void printGraph() {
System.out.println("Graph representation using Adjacency Matrix:");
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
System.out.print(adjMatrix[i][j] ? "1 " : "0 ");
}
System.out.println();
}
}
// Get the number of vertices
public int getVertexCount() {
return V;
}
// Example usage
public static void main(String[] args) {
// Create a graph with 5 vertices
GraphMatrix graph = new GraphMatrix(5);
// Add edges
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);
// Print the graph
graph.printGraph();
}
}
Implement a weighted graph class that supports the following operations:
Implement both adjacency list and adjacency matrix versions.
Implement the following methods for your graph class:
Solve the following graph problems: