Introduction to Graph Data Structures

Graph Fundamentals

  • A graph is a collection of nodes called vertices and the connections between them called edges, denoted as G=(V,E)G = (V, E).

  • An edge is a pair of vertices e=(u,v)e = (u, v). Entities such as people, cities, or computers are represented by vertices, while edges represent their relationships.

  • Directed Graph (Digraph): A graph where every edge has a specific direction.

  • Undirected Graph: A graph where edges have no direction.

Graph Terminologies

  • Adjacent Vertices: Two vertices connected by an edge. In a directed graph, if (x,y)(x, y) is an edge, xx is adjacent to yy, and yy is adjacent from xx.

  • Degree: The number of edges incident to a vertex. The sum of the degrees of all vertices is equal to 2×2 \times the number of edges.

  • Path: A sequence of vertices V1,V2,…,VkV_1, V_2, \dots, V_k where consecutive vertices are adjacent.

  • Simple Path: A path in which no vertex is repeated.

  • Cycle: A simple path where the first and last vertices are the same.

  • Connected Graph: A graph where a path exists between every pair of vertices.

  • Complete Graph: A graph where there is an edge between every pair of vertices.

  • Tree and Forest: A tree is a connected graph with no cycles. A forest is a collection of trees.

  • Subgraph: A subset of vertices and edges that forms a graph. A connected component is a maximal connected subgraph.

Applications and Representations

  • Applications: Designing circuits to find the path of least resistance and modeling networks (roads, flights, or communications) to determine shortest paths.

  • Adjacency Matrix: An n×nn \times n matrix AA where A[i][j]=1A[i][j] = 1 if an edge exists between node ii and node jj, and 00 otherwise.

  • Adjacency Lists: A one-dimensional array LL of linked lists, where L[i]L[i] contains all nodes adjacent from vertex ii.

Graph Traversal Techniques

  • Depth-First Search (DFS):

    • Implements a "last visited, first to backtrack" strategy using a stack.

    • Rules: Visit an adjacent unvisited vertex, mark it as visited, display it, and push it onto the stack. If no adjacent unvisited vertex exists, pop from the stack.

  • Breadth-First Search (BFS):

    • Visits nodes level by level using a queue.

    • Rules: Visit an adjacent unvisited vertex, mark it as visited, display it, and insert it into the queue. If no adjacent unvisited vertex exists, remove the first vertex from the queue.

Questions & Discussion

  • Draw the directed graph corresponding to a given adjacency matrix (e.g., involving Boolean values True/False).

  • Draw the edge lists (adjacency lists) for a specific graph.

  • Determine the order of vertex visits for a depth-first search starting at vertex v0v_0.

  • Determine the order of vertex visits for a breadth-first search starting at vertex v0v_0.

  • Define the terms Edge, Vertex, Path, and Degree.