Computer Science 301 - Data Structures and Algorithms - Lecture Notes
Computer Science 301 - Data Structures and Algorithms - Lecture Notes
Lecture Week 7: Graph Algorithms and Their Applications Professor Martinez - Fall 2024
1. Adjacency Matrix
# Example of an adjacency matrix
graph = [
[0, 1, 0, 1],
[1, 0, 1, 0],
[0, 1, 0, 1],
[1, 0, 1, 0]
Space Complexity: O(V²) Time Complexity for checking edge existence: O(1)
2. Adjacency List
# Example of an adjacency list
graph = {
Space Complexity: O(V + E) Time Complexity for checking edge existence: O(V)
visited = set()
queue = [start]
visited.add(start)
while queue:
vertex = queue.pop(0)
visited.add(neighbor)
queue.append(neighbor)
if visited is None:
visited = set()
visited.add(start)
- BFS: O(V + E)
- DFS: O(V + E)
- Dijkstra's Algorithm: O((V + E) log V) with priority queue
- Floyd-Warshall: O(V³)
V. Practice Problems
1. Implement a function to detect cycles in a directed graph
2. Find the shortest path between two vertices using BFS
3. Determine if a graph is bipartite
1. Implement both BFS and DFS for a graph represented as an adjacency matrix
2. Write a function to convert between adjacency matrix and adjacency list representations
3. Solve the "Number of Islands" problem on LeetCode (#200)
Important Dates
- Midterm Exam: October 15th
- Project Proposal Due: October 20th
- Final Project Presentations: December 5th
Remember: The key to understanding graph algorithms is practicing their implementation and
analyzing their behavior on different types of graphs.
Additional Resources
- Introduction to Algorithms (CLRS) - Chapter 22: Graph Algorithms
- Stanford's CS161 Course Notes
- Visualgo.net for algorithm visualization
- LeetCode's Graph Theory Problem Set
These notes were taken during lecture. Please refer to the course syllabus for official
assignment details and deadlines.