Prev: W5, Next: W7
Links: Zoom, TopHat (010339, or Form), Calculator:
Slide:



# Graph

📗 Graphs consist of:
➩ Set of nodes (or vertices).
➩ Set of links (or edges).
📗 Two nodes connected with direct edge are:
➩ Neighbors or adjacent nodes.



# Directed Graphs

📗 Two types of graphs: undirected and directed.
📗 Degree of a node: the number of edges connected to a node.
➩ In degree and out degree for directed graphs.

# Adjacency Matrix Example

ID:
📗 [1 point] Write down the to create the graph on the right.
➩ Node names:

📗 List: (Click anywhere on the left to draw the graph)

# Adjacency List Example

ID:
📗 [1 point] Write down the to create the graph on the right.
➩ Node names:

📗 List: (Click anywhere on the left to draw the graph)



# Paths in a Graph

📗 Cycle: A path that returns to a previously visited node.
📗 Cyclic graph: contains at least one cycle.
📗 Acyclic graph: contains no cycle.



# Graph Traversals

📗 Goal: Traverse graph by crossing edges to visit each node exactly once.
📗 Points to consider:
➩ Need to pick starting node.
➩ Nodes might be unreachable from starting node.
➩ Cycles can lead to infinite loops.



# Detecting Cycles

📗 Detect cycles to avoid visiting nodes multiple times.
📗 Strategy: check if node is unvisited before visiting it.
➩ Need to keep track of visited nodes.
➩ Either with Boolean field in node type, or,
➩ Additional data structure to keep track of visited nodes.



# Depth First Traversal

📗 Assume: all vertices are marked unvisited at start.
📗 DFT(v):
mark v as visited
for each unvisited neighbor u of v:
  DFT(u)
📗 Visualization: Link.


# Depth First Example

ID:
📗 [1 point] List the nodes in use Traversal.
➩ Node names: and Adjacency matrix:

📗 Queue:
📗 Nodes:



# Breadth First Traversal

📗 Assume: all vertices are marked unvisited at start.
📗 BFT(v):
q = new Queue()
mark v as visited
q.enqueue(v)
while (!q.isEmpty()):
  c = q.dequeue()
  for each unvisited neighbor u of c:
    mark u as visited
    q.enqueue(u)
📗 Visualization: Link.


# Breadth First Example

ID:
📗 [1 point] List the nodes in use Traversal.
➩ Node names: and Adjacency matrix:

📗 Queue:
📗 Nodes:



# Time Complexity

📗 V: the number of nodes in graph.
📗 E: the number of edges in graph.
📗 Depth first:
➩ Adjacency matrix: \(O\left(V^{2}\right)\).
➩ Adjacency list: \(O\left(V + E\right)\).
📗 Breadth first:
➩ Adjacency matrix: \(O\left(V^{2}\right)\).
➩ Adjacency list: \(O\left(V + E\right)\).



# Connected Graph

📗 In a connected graph, a path exists between every pair of nodes.
📗 For directed graphs:
➩ Strongly connected: a path exists between every pair of nodes with edge directions respected.
➩ Weakly connected: a path exists between every pair of nodes with edge directions ignored.



# Subgraphs

📗 G' is a subgraph of G if:
➩ The set of nodes of G' is a subset of the nodes of G, and,
➩ The set of edges of G' is a subset of the edges of G.



# Spanning Tree

📗 For an undirected graph G with V nodes, T is a spanning tree of G if:
➩ T is a subgraph of G,
➩ T contains all nodes of G,
➩ T is connected, and,
➩ T has exactly (V - 1) edges.
📗 Example: mazes are random spanning trees, Link.



# Depth First Spanning Trees

📗 DFT(v):
mark v as visited
for each unvisited neighbor u of v:
  add v-u to spanning tree (*)
  DFT(u)



# Breadth First Spanning Trees

📗 BFT(v):
q = new Queue()
mark v as visited
q.enqueue(v)
while (!q.isEmpty()):
  c = q.dequeue()
  for each unvisited neighbor u of c:
    add v-u to spanning tree (*)
    mark u as visited
    q.enqueue(u)



# Readings

📗 Graphs: Link



📗 Notes and code adapted from the course taught by Professor Florian Heimerl and Ashley Samuelson.
📗 Please use Ctrl+F5 or Shift+F5 or Shift+Command+R or Incognito mode or Private Browsing to refresh the cached JavaScript.
📗 You can expand all the examples and demos: , or print the notes: .
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google Form at the end of the lecture.

Prev: W5, Next: W7





Last Updated: November 25, 2025 at 1:46 AM