COSC 2436: Graphs
What is a graph?
• A graph is a data structure for representing connections among items,
and consists of vertices connected by edges
• Vertex (or node) - represents an item in a graph
• Edge - represents a connection between two vertices in a graph
• Two vertices are adjacent if connected by an edge
• Path - a sequence of edges leading from a source (starting) vertex to a
destination (ending) vertex
• Length - number of edges in the path
Graph
Directed Graph
• A directed graph consists of vertices connected by directed edges
• Directed edge - a connection between a starting vertex and an
ending vertex
• In a directed graph, vertex Y is adjacent to vertex X, if there is an
edge from X to Y
Directed Graph
Weighted Graphs
• A weighted graph associates a weight with each edge
• Weight (or cost) - represents some numerical value between
vertices
• A weighted graph may be directed or undirected
• In a weighted graph, the path length is the sum of the edge
weights in the path
Weighted Graph
Graphs: Prims Algorithm
Prim’s algorithm nds the minimum spanning tree (MST) of a
graph.
Prim’s algorithm starts from a speci c starting vertex and then it
chooses the next vertex with the smallest weight (or cost) that
doesn’t create a cycle. It continues this process until all vertices
have been visited.
fi
fi
Graphs: Prims Algorithm
Find the minimum spanning tree (MST) of the following graph
starting from vertex A.
19 9
A B C
14
8 16 10
11 17 E
G F D 18
12 14
Graphs: Prims Algorithm
Find the minimum spanning tree (MST) of the following graph
starting from vertex A.
9
A B C
14
8 10
11 E
G F D
12
Graphs: Kruskal’s Algorithm
Kruskal’s algorithm nds the minimum spanning tree (MST) of a
graph.
Kruskal’s algorthim rst lists out all of the edges from least to
greatest (in terms of their weights). It then chooses the edge with
the smallest weight which doesn’t form a cycle. It repeats this
until all vertices have been visited.
fi
fi
Graphs: Kruskal’s Algorithm
Find the minimum spanning tree (MST) of the following graph
using Kruskal’s algorithm.
8 B 5
A C
3 4
H 7
E 8
12
3 2
G D
F
10 10
Graphs: Kruskal’s Algorithm
Find the minimum spanning tree (MST) of the following graph
using Kruskal’s algorithm.
8 B 5
A C
3
H
E 8
3 2
G D
F
10
Graphs: Dijkstra’s Algorithm
Find the shortest path tree from vertex A
3 7
D E F
2 3
5 1 7
C B A
2 2
12
Graphs: Dijkstra’s Algorithm
Find the shortest path tree from vertex A
A B C D E F
3 0 ∞ ∞ ∞ ∞ ∞
D E F
2 12 ∞ ∞ 7
3
1 4 ∞ 3 5
4 6 5
C B A
2 2 6 5
6
Graph Implementations:
There are two main ways in which graphs are implemented:
• Adjacency List - an array of linked list where each index in the
array represents each vertex in the graph and the linked list
represent the edges of that vertex.
• Adjacency Matrix (also called Incidence Matrix) - a matrix
where typically the rows represent the vertices and the
columns represent the edges
Adjacency Matrix
0
0 1 1 1 0
1 1 0 1 0 1
2 3
1 1 0 0 0
4
1 0 0 0 1
0 1 0 1 0
Adjacency List
0
1 2 3 NULL
1 0 2 4 NULL
2 3
0 1 NULL
4
0 4 NULL
1 3 NULL
Graphs: Breadth-first-search (BFS)
• Breadth rst search (BFS) - a graph traversal that visits a starting
vertex, then all vertices of “distance” 1 from that vertex, then
distance 2, and so on
fi
Graphs: BFS Algorithm
• Uses a queue
• Enqueue the starting vertex
• While the queue is not empty, you should:
• dequeue the vertex from the front of the queue
• visit the dequeued vertex
• enqueues that vertex’s adjacent vertices (if not already discovered)
• repeat until all vertices have been visited
Graphs: BFS for Adjacency List
Graphs: BFS for Adjacency Matrix
Graphs: Depth-first-search (DFS)
• Depth rst search (DFS) - a graph traversal that visits a starting
vertex, then visits every vertex along each path starting from that
vertex to the path’s end before backtracking.
fi
Graphs: DFS Algorithm
• DFS uses a stack
• Push the source vertex onto the stack
• While the stack is not empty:
• pop from the stack
• if the vertex has not been visited yet then print the vertex
• push all adjacent vertices onto the stack
• repeat until all vertices have been visited
Graphs: DFS for Adjacency List
Graphs: DFS for Adjacency Matrix