Graph Theory and Searching Algorithms Review
Graph Theory Fundamentals: Adjacency Matrix and Adjacent Nodes
Definition of Adjacent Nodes:
- In a graph , two nodes (or vertices) and are said to be adjacent if there is an edge or directly linking them.
- If the graph is undirected, adjacency is mutual: if is adjacent to , then is adjacent to .
- If the graph is directed, node is adjacent to if there is a directed edge from to .
Definition of Adjacency Matrix:
- An Adjacency Matrix is a mathematical way of representing the connections (edges) between the nodes of a graph in a square matrix format.
- For a graph with nodes, the adjacency matrix is an matrix where:
- if there is an edge from node to node .
- if there is no edge between node and node .
- Properties for Undirected Graphs: The matrix is symmetric () because an edge between and implies a connection in both directions.
- Properties for Directed Graphs: The matrix may or may not be symmetric, depending on the presence of reciprocal edges.
- Diagonal Elements: If a node has a self-loop (an edge connecting a node to itself), the diagonal element will be . Otherwise, it is typically .
Searching Techniques: Linear and Binary Search
Definition of Searching:
- Searching is the process used to find the location or existence of a specific element (target/key) within a given collection of data, such as an array, list, or tree.
Linear Search:
- Mechanism: This algorithm starts at the beginning of the data set and examines each element sequentially one by one until the target is found or the end of the collection is reached.
- Requirement: The data does not need to be sorted.
- Example: Finding the number in the list . The search checks index , then index , then finds at index .
- Time Complexity:
- Best Case: (target is at the first position).
- Average/Worst Case: (target is at the end or not present).
Binary Search:
- Mechanism: A divide-and-conquer algorithm that repeatedly divides the search interval in half. It compares the target value to the middle element of the array.
- Requirement: The data must be sorted in ascending or descending order.
- Steps:
- Start with the middle element.
- If the target is equal to the middle element, the search is complete.
- If the target is smaller, repeat the process on the left half.
- If the target is larger, repeat the process on the right half.
- Example: Finding the number in a sorted list .
- Sorted List:
- Middle is . Since , search the right half: .
- Middle is . Target found.
- Time Complexity:
- Best Case: .
- Average/Worst Case: .
Differentiation of Time Complexities:
- Efficiency: Binary search () is significantly faster than Linear search () for large datasets.
- Example Comparison: In a dataset of elements, Linear Search might take up to steps, whereas Binary Search would take at most around steps.
Construction of an Undirected Graph and Adjacency Matrix
Specification of Graph G:
- Vertices
- Edges
Graph Visualization (Undirected):
- Node 1 is connected to: 2, 3, and 4.
- Node 2 is connected to: 1.
- Node 3 is connected to: 1, 3 (self-loop), and 4.
- Node 4 is connected to: 3 and 1.
Adjacency Matrix for Graph G:
- Since the graph is undirected, every edge creates an entry for both and . The self-loop creates a single entry at .
Directed vs. Undirected Graphs and Node Degrees
Directed Graph (Digraph):
- A graph where edges have a specific direction. Edges are represented by ordered pairs , indicating a path from to .
Undirected Graph:
- A graph where edges have no direction. Edges are unordered pairs, meaning the connection is the same as .
Finding In-degree and Out-degree (Directed Graph):
- In-degree: The number of edges coming into a specific node. In an adjacency matrix, this is the sum of the columns for that node.
- Out-degree: The number of edges going out from a specific node. In an adjacency matrix, this is the sum of the rows for that node.
Degree Calculation Example:
- Suppose a graph with edges: .
- Node 1:
- Out-degree: (Edges to 2 and 3)
- In-degree: (Edge from 3)
- Node 2:
- Out-degree: (Edge to 3)
- In-degree: (Edge from 1)
- Node 3:
- Out-degree: (Edge to 1)
- In-degree: (Edges from 1 and 2)
Spanning Trees and Minimum Cost Spanning Tree (MCST)
What is a Spanning Tree?
- A spanning tree is a subset of a connected, undirected graph that includes all the vertices of the graph and forms a tree (no cycles).
- If a graph has vertices, the spanning tree must have exactly edges.
Minimum Cost Spanning Tree (MCST):
- In a weighted graph, the MCST is the spanning tree where the sum of the weights of the edges is minimized.
Algorithm to find MCST (Kruskal's Algorithm):
- Sort all the edges of the graph in non-decreasing order of their weight.
- Pick the smallest edge. Check if it forms a cycle with the spanning tree formed so far. If a cycle is not formed, include this edge.
- Repeat step 2 until there are edges in the spanning tree.
Example of MCST Algorithm:
- Consider a graph with edges: .
- Step 1 (Sort Edges): .
- Step 2: Add ; total cost = .
- Step 3: Add ; no cycle formed; total cost = .
- Step 4: Consider ? Adding creates a cycle . Discard it.
- Step 5: Add ; no cycle formed; total cost = .
- Final MCST edges: .
Graph Traversal Algorithms: BFS and DFS
Breadth-First Search (BFS):
- Concept: Explores neighbors before moving to the next level (Layer-by-layer exploration).
- Data Structure: Uses a Queue (First-In-First-Out).
- Algorithm:
- Maintain a queue and a list of visited nodes.
- Enqueue the starting node and mark it as visited.
- De-queue a node, visit all its unvisited adjacent neighbors, mark them as visited, and enqueue them.
- Repeat until the queue is empty.
- Example: From Node 1 in a graph with connections .
- BFS Order: .
Depth-First Search (DFS):
- Concept: Explores as far as possible along each branch before backtracking.
- Data Structure: Uses a Stack or Recursion (Last-In-First-Out).
- Algorithm:
- Maintain a stack and a list of visited nodes.
- Push the starting node onto the stack.
- Pop a node, if not visited, mark it as visited.
- Push all its unvisited neighbors onto the stack.
- Repeat until the stack is empty.
- Example: From Node 1 in a graph with connections .
- DFS Order: (depending on push order).