Back to Welcome Page

Graphs I: Introduction to Graph Theory

Learn the fundamentals of graph theory and explore graph representations. Understand how graphs can model complex relationships across various domains.

Learning Objectives

Core Concepts

  • Understanding graph terminology
  • Exploring types of graphs: directed, undirected, weighted
  • Analyzing graph properties
  • Learning graph representation techniques

Skills Development

  • Implementing adjacency lists and matrices
  • Building simple graph applications
  • Modeling real-world problems as graphs
  • Understanding graph use cases

Prerequisites

Required Knowledge

  • Basic data structures (arrays, lists)
  • Object-oriented programming concepts
  • Java Collection Framework basics
  • Understanding of time complexity

Recommended Preparation

  • Review of HashMap implementation
  • Refresh on ArrayList operations
  • Understanding of classes and objects
  • Familiarity with iterative algorithms

Video Lectures

Introduction to Graph Theory

Key Concepts Covered:

  • Vertices and edges
  • Directed vs. undirected graphs
  • Weighted vs. unweighted graphs
  • Applications of graph theory

Graph Representation Techniques

Key Concepts Covered:

  • Adjacency matrix representation
  • Adjacency list representation
  • Edge list representation
  • Time and space complexity comparison

Graph Terminology and Examples

Key Graph Terms

  • Vertex (Node): A fundamental unit in a graph that represents an entity
  • Edge: A connection between two vertices representing a relationship
  • Degree: The number of edges connected to a vertex
  • Path: A sequence of vertices connected by edges
  • Cycle: A path that starts and ends at the same vertex
  • Connected Graph: A graph where there is a path between every pair of vertices
  • Tree: A connected graph with no cycles
  • Bipartite Graph: A graph whose vertices can be divided into two sets with no edges within each set
Example of an undirected graph

Example of an undirected graph with 6 vertices and 7 edges

Adjacency List Implementation

Java Implementation

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();
    }
}

Guided Practice

For additional hands-on practice and exercises on graph theory, check out our comprehensive guided project:

Adjacency Matrix Implementation

Java Implementation

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();
    }
}

Practice Exercises

Basic Graph Implementation

Implement a weighted graph class that supports the following operations:

  • Add a weighted edge between two vertices
  • Remove an edge between two vertices
  • Check if there is an edge between two vertices
  • Get all neighbors of a vertex with their weights
  • Print the graph representation

Implement both adjacency list and adjacency matrix versions.

Graph Property Analysis

Implement the following methods for your graph class:

  • Check if a graph is connected
  • Find the degree of each vertex
  • Determine if the graph has a cycle
  • Count the number of components in the graph

Real-World Application Challenges

Solve the following graph problems:

Additional Resources