Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Computer Science 301 - Data Structures and Algorithms - Lecture Notes

Uploaded by

nihed13535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Computer Science 301 - Data Structures and Algorithms - Lecture Notes

Uploaded by

nihed13535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS301: Data Structures and Algorithms

Lecture Week 7: Graph Algorithms and Their Applications Professor Martinez - Fall 2024

I. Review of Graph Fundamentals


Today we continued our exploration of graph theory and its practical applications. Remember
that a graph G is defined by G = (V,E) where:

- V represents the set of vertices (nodes)


- E represents the set of edges (connections between nodes)

Types of Graphs Covered:


1. Undirected Graphs

- Edges have no direction


- If v1 is connected to v2, then v2 is connected to v1
- Example: Facebook's friendship network

2. Directed Graphs (Digraphs)

- Edges have direction


- If v1 points to v2, v2 may not point to v1
- Example: Twitter's follower network

II. Graph Representations


We discussed two primary ways to represent graphs in code:

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 = {

'A': ['B', 'D'],

'B': ['A', 'C'],

'C': ['B', 'D'],

'D': ['A', 'C']

Space Complexity: O(V + E) Time Complexity for checking edge existence: O(V)

III. Graph Traversal Algorithms

Breadth-First Search (BFS)


Key Points:

- Uses a queue data structure


- Explores all neighbors at current depth before moving deeper
- Optimal for finding shortest paths in unweighted graphs

def bfs(graph, start):

visited = set()

queue = [start]

visited.add(start)
while queue:

vertex = queue.pop(0)

print(vertex, end=' ')

for neighbor in graph[vertex]:

if neighbor not in visited:

visited.add(neighbor)

queue.append(neighbor)

Depth-First Search (DFS)


Key Points:

- Uses a stack (or recursion)


- Explores as far as possible along each branch before backtracking
- Useful for topological sorting and cycle detection

def dfs(graph, start, visited=None):

if visited is None:

visited = set()

visited.add(start)

print(start, end=' ')

for neighbor in graph[start]:

if neighbor not in visited:


dfs(graph, neighbor, visited)

IV. Time Complexity Analysis


Important complexities to remember:

- 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

VI. Homework Assignment


Due next week:

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.

You might also like