Definition of Graphs
• A graph is a finite set of nodes with edges
between nodes
• Formally, a graph G is a structure (V,E)
consisting of
– a finite set V called the set of nodes, and
– a set E that is a subset of VxV. That is, E is a set
of pairs of the form (x,y) where x and y are nodes
in V
1
Motivation
•
•
•
•
•
•
What are some characteristics of a binary tree?
– They have a rigid structure of each node having a single node that points to it (or
none, in the case of the root). Sometimes life isn’t so structured.
For example: I need to fly to Tokyo. I want to find the cheapest way to fly there. How
could I think about the data?
For example:
– I have a set of candy bars in this bag. I want to distribute so everybody gets a kind
they like. How do I represent the data?
Human graph. If you could pick one person who you would go to for help with an
assignment, who would it be? Using your arm as a link, touch that person on the
shoulder. What if you could choose two people?
For example: I want to travel every bike path in Logan (assuming they had any ) but
I don’t want to ever travel on the same path twice. Can I begin at my home, visit every
path, and then return?
The data representation in all of these cases is the graph!
2
Terms
•
•
•
•
Graph – set of vertices and set of edges.
– More formally, a graph is an ordered pair of finite sets V and E.
• The set V is the set of vertices.
• The set E is the set of edges.
• Sometimes the edges have weight (termed a weighted graph)
• Sometimes the edges have direction (I can go from A to B but not B to
A). A graph with directed edges is termed a digraph or directed graph.
Otherwise, we call it an undirected graph.
Successor – the follows relationship in a directed graph.
Degree – the number of edges incident to or touching a vertex.
– In-degree – the number of edges coming into a vertex (digraph only).
– Out-degree – the number of edges going out of a vertex (digraph only).
Predecessor – the precedes relationship in a digraph.
3
• Draw a graph of five people around you
now. The people become the nodes. Let
edges be “is born in the same year as you”.
– Is this directed or undirected?
• Is in connected? (A connected graph is a
graph that you can get from a node to every
other node following edges
4
Examples of Graphs
“Want to work with”
• V={0,1,2,3,4}
• E={(0,1), (1,2), (0,3), (3,0), (2,2), (4,3)}
1
0
2
4
3
When (x,y) is an edge,
we say that x is adjacent to
y, and y is adjacent from x.
0 is adjacent to 1.
1 is not adjacent to 0.
2 is adjacent from 1.
5
A “Real-life” Example of a Graph
• V=set of 6 people: John, Mary, Joe, Helen,
Tom, and Paul, of ages 12, 15, 12, 15, 13,
and 13, respectively.
• E ={(x,y) | if x is younger than y}
Mary
Helen
Joe
John
Tom
Paul
6
Intuition Behind Graphs
• The nodes represent entities (such as people, cities,
computers, words, etc.)
• Edges (x,y) represent relationships between entities x and
y, such as:
–
–
–
–
–
–
–
“x loves y”
“x hates y”
“x is a friend of y” (note that this not necessarily reciprocal)
“x considers y a friend”
“x is a child of y”
“x is a half-sibling of y”
“x is a full-sibling of y”
• In those examples, each relationship is a different graph
7
Graph Representation
• For graphs to be computationally useful,
they have to be conveniently represented in
programs
• There are two computer representations of
graphs:
– Adjacency matrix representation
– Adjacency lists representation
8
Adjacency Matrix Representation
• In this representation, each graph of n nodes
is represented by an n x n matrix A, that is,
a two-dimensional array A
• The nodes are (re)-labeled 1,2,…,n
• A[i][j] = 1 if (i,j) is an edge
• A[i][j] = 0 if (i,j) is not an edge
9
Example of Adjacency Matrix
0
0
A=
0
1
0
1
0
0
0
0
0
1
1
0
0
1
0
0
0
1
0
0
0
0
0
1
0
2
4
3
10
Another Example of Adj. Matrix
• Re-label the nodes with numerical labels
0
0
A=
1
1
1
1
0
0
1
1
1
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
Mary 0
0
0
1
John 2
1
0
Tom 4
0
Helen 1
Joe 3
Paul 5
11
Pros and Cons of Adjacency
Matrices
• Pros:
– Simple to implement
– Easy and fast to tell if a pair (i,j) is an edge:
simply check if A[i][j] is 1 or 0
• Cons:
– No matter how few edges the graph has, the
matrix takes O(n2) in memory
12
Adjacency Lists Representation
• A graph of n nodes is represented by a onedimensional array L of linked lists, where
– L[i] is the linked list containing all the nodes
adjacent from node i.
– The nodes in the list L[i] are in no particular
order
13
Example of Linked Representation
L[0]: empty
L[1]: empty
L[2]: 0, 1, 4, 5
L[3]: 0, 1, 4, 5
L[4]: 0, 1
L[5]: 0, 1
Mary 0
Helen 1
Joe
3
John 2
Tom 4
Paul 5
14
class EdgeNode
{ public:
int toNode; // Node id of target of edge
int fromNode; // Node id of source of edge
int distance; // Cost of Edge (for a weighted graph only)
EdgeNode *next; // Next edge in linked list for adjacency list
representation
// Constructor – note the use of default parameters.
// Note the strange assignments (distance(dist)) – they aren’t my favorite
either, but texts seem to like them
EdgeNode( int from = -1, int to=-1, int dist=0, EdgeNode *nextEdge =
NULL):
distance(dist),toNode( to ), fromNode(from), next(nextEdge){};
}; // EdgeNode
15
class GraphNode
{ public:
inf nodeID;
// ID of node; something that makes it easy to find the node in the array
string name; // Node name
EdgeNode *adj; // Adjacency list
bool visited; // true if visited already
GraphNode( int id= -1, string nameEle=" ", EdgeNode *adjList = NULL):
nodeID(id), name(nameEle), adj(adjList){ visited = false;}
}; // GraphNode
class Graph
{ protected:
GraphNode *G; // Array of nodes of graph – will be given space once the size is known
int nodeCt; // Size of G
public:
Graph(int size ) { G = new GraphNode[size]; nodeCt = size;}; // create node array
string toString(string label,ostream & fout);
void BuildGraph(istream & inf);
}; // Graph
16
Directed vs. Undirected Graphs
• If the directions of the edges matter, then
we show the edge directions, and the graph
is called a directed graph (or a digraph)
• If the relationships represented by the edges
are symmetric (such as (x,y) is edge if and
only if x is a sibling of y), then we don’t
show the directions of the edges, and the
graph is called an undirected graph.
17
Examples of Undirected Graphs
• V=set of 6 people: John, Mary, Joe, Helen,
Tom, and Paul, where the first 4 are siblings,
and the last two are siblings
• E ={(x,y) | x and y are siblings}
Mary
John
Tom
if (x,y) is an edge:
we say that x is
Joe adjacent to y, &
y adjacent to x.
We also say that
x and y are
Paul
neighbors
Helen
18
Representations of Undirected
Graphs
• The same two representations for directed
graphs can be used for undirected graphs
• Adjacency matrix A:
– A[i][j]=1 if (i,j) is an edge; 0 otherwise
• Adjacency Lists:
– L[i] is the linked list containing all the
neighbors of i
19
Example of Representations
Linked Lists:
L[0]: 1, 2, 3
L[1]: 0, 2, 3
L[2]: 0, 1, 3
L[3]: 0, 1, 2
L[4]: 5
L[5]: 4
Mary 0
Helen 1
John 2
Joe 3
Tom 4
Adjacency Matrix:
0
1
A=
1
1
0
0
1
0
1
1
0
0
1
1
0
1
0
0
1
1
1
0
0
0
0
0
0
0
0
1
Paul 5
0
0
0
0
1
0
20
Definition of Some Graph Related
Concepts
• Let G be a directed graph
– The indegree of a node x in G is the number of
edges coming to x
– The outdegree of x is the number of edges
leaving x.
• Let G be an undirected graph
– The degree of a node x is the number of edges
that have x as one of their end nodes
– The neighbors of x are the nodes adjacent to x
21
Things for You To Do
• Add a member function to the class graph,
called getIndegree( int x), which returns the
indegree of node x
• Add a member function to the class graph,
called getOutdegree( int x), which returns
the outdegree of node x
22
Pros and Cons of Adjacency Lists
• Pros:
– Saves on space (memory): the representation
takes as many memory elements as there are
nodes and edge.
• Cons:
– It can take up to O(n) time to determine if a pair
of nodes (i,j) is an edge: one would have to
search the linked list L[i], which takes time
proportional to the length of L[i].
23
Paths
• A path in a graph G is a sequence of nodes x1,
x2, …,xk, such that there is an edge from each
node the next one in the sequence
• For example, in the first example graph, the
sequence 3, 0, 1, 2 is a path, but the sequence
0, 3, 4 is not a path because (0,3) is not an edge
• In the “sibling-of” graph, the sequence John,
Mary, Joe, Helen is a path, but the
sequence Helen, Tom, Paul is not a path
24
Graph Connectivity
• An undirected graph is said to be connected
if there is a path between every pair of
nodes. Otherwise, the graph is disconnected
• Informally, an undirected graph is
connected if it hangs in one piece
Disconnected
Connected
25
Connected Components
• If an undirected graph is not connected, then each
“piece” is called a connected component.
• If the graph is connected, then the whole graph is
one single connected component
• Of Interest: Given any undirected graph G,
– Is G connected?
– If not, find its connected components.
26
Strongly Connected Components
• Every pair of vertices are reachable from each other
• Graph G is strongly connected if, for every u and v in
V, there is some path from u to v and some path from v
to u.
Strongly
Connected
Not Strongly
Connected
Example of Strongly Connected
Components (SCC)
a
g
c
d
f
e
{a,c,g}
{f,d,e,b}
b
We say a graph is weakly connected if the graph
(ignoring directions) is connected
Sometimes we shrink a SCC into
one “supernode”
29
Subgraph
• Suppose the graph below represents one way streets in a town and the
nodes represent hotels.
• Maybe I only care about hotels 0,1,3,6 in the figure. I could take those
nodes and the edges between them and talk about the subgraph.
• Figure
• Subgraph – a graph that consists of a subset of vertices and a subset of
edges between the vertices
30
• By definition a graph does not contain:
– Multiple copies of the same edge.
– Self-edges – edges of the form . This is also called a loop.
• If we need multiple edges between nodes, it is termed a multigraph.
• In our hotel example, multiple edges between 6 and 5 could represent two
different road between the hotels.
• Paths
– Path – a sequence of edges (one ends where next begins)
– Simple path – there are no repeated vertices or edges.
– Length of a path – the sum of the lengths of the edges on the path.
• Sometimes we want to talk about cycles (a path that begins and ends at the
same place)
• Simple cycle – a cycle with no repeated vertices (except at the beginning
and the end).
– The cycle (0320) in the graph on previous slide is a simple
31
cycle.
Graph Traversal Techniques
• The previous connectivity problem, as well
as many other graph problems, can be
solved using graph traversal techniques
• There are two standard graph traversal
techniques:
– Depth-First Search (DFS)
– Breadth-First Search (BFS)
32
Output
List:
Depth First Search
1
2
4
3
7
6
10
13
9
11
8
5
15
14
12
follow successors in
numeric order
1
2
5
6
3
4
8
10
7
11
15
13
9
12
14
• Start with 1
• 2,4,3
– Stop at 3
• Backtrack to 4, can we search?
• 7,6
– Stop at 2
• Backtrack to 6, can we search?
• 10,13
– Stop at 13
• Backtrack to 10,6,7
• 9, 11, 8, 5
– Stop at 5
• Backtrack to 8,11
• 15,14,12
– STOP – All nodes are marked!!
Depth First Search – Real Life Application
From
xkcd.com
Graph Traversal (Contd.)
• In both DFS and BFS, the nodes of the
undirected graph are visited in a systematic
manner so that every node is visited exactly
once.
• Both BFS and DFS consider an embeded
tree:
– When a node x is visited, it is labeled as visited,
and it is added to the tree
– If the traversal got to node x from node y, y is
viewed as the parent of x, and x a child of y
35
Depth-First Search
•
DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the
current node
2. Find an unvisited neighbor of the current node, visit it,
and make it the new current node;
3. If the current node has no unvisited neighbors,
backtrack to the its parent, and make that parent the
new current node;
4. Repeat steps 3 and 4 until no more nodes can be
visited.
5. If there are still unvisited nodes, repeat from step 1.
Generates a forest.
36
At seats
• Write recursive code to do a DFS search
37
Breadth-First Search
• BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root
in a BFS tree being formed. Its level is called the
current level.
2. Try all one-step extensions of current
paths before trying larger extensions.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.
38
Illustration of BFS
1
0
0
2
9
1
5
4
10
2
4
9
10
6 7 8
11
BFS Tree
7
5
11
Graph G
6
8
39
Breadth First Search
1
2
5
6
3
•
•
•
•
•
L0:
L1:
L2:
L3:
L4:
4
7
8
10
11
15
13
9
12
14
1
2, 3
4,5,6,11
7,8,10,9,15
13,12,14
Final output: 1, 2, 3, 4, 5, 6, 11, 7, 8, 10, 9, 15, 13, 12, 14
Topological Ordering
• In college, before taking a particular course, students usually must take all
prerequisite courses.
• For example, before taking the CS II course, students must take the CS I
course.
• However, certain courses can be taken independently of each other.
• The courses within a department can be represented as a directed graph.
• A directed edge from, say, vertex u to vertex v means that the course
represented by the vertex u is a prerequisite of the course represented by
the vertex v.
• It would be helpful for students to know, before starting a major, the
sequence in which they can take courses so that before taking a particular
course they take all its prerequisite courses.
• Let G be a directed graph and V(G) = {v1, v2, ..., vn}, where
n > 0.
• A topological ordering of V(G) is a linear ordering vi1, vi2,
..., vin of the vertices such that if vij is a predecessor of vik, j
k, 1 j n, and 1 k n, then vij precedes vik, that is, j <
k in this linear ordering.
• We assume that the graph has no cycles.
• Because the graph has no cycles:
• There exists a vertex u in G such that u has no predecessor.
• There exists a vertex v in G such that v has no successor.
What algorithm would you use to
find a topological order?
43
• In the breadth first topological ordering we first find a vertex
that has no predecessor vertex and place it first in the topological
ordering.
• We next find the vertex, say v, all of whose predecessors have
been placed in the topological ordering and place v next in the
topological ordering.
• To keep track of the number of vertices of a vertex we use the
array predCount.
• Initially, predCount[j] is the number of predecessors of the
vertex vj.
• The queue used to guide the breadth first traversal is initialized
to those vertices vk such that predCount[k] is zero.
• The general algorithm is:
• The vertices of G3 in a breadth first topological ordering
COULD BE
0 9 1 7 2 5 4 6 3 8 10
• We illustrate the breadth first topological ordering of the graph
G3
Big Picture pseudocode
Look at each edge. Count the number of predecessors of each node.
For each node with predCt[node] ==0, put q.enque(node);
While (!q.empty()){
curr = q.deque();
cout << curr; // output nodes in topo order
for each successor, succ of curr,
{predCt[succ]—
if predCt[succ]==0) q.enque(succ)
}
}
52
Shortest-Path Algorithms
• The input is a weighted graph: associated with
each edge (vi, vj) is a cost ci,j.
• The cost of a path is the sum of the weights of
the edges weighted path length
• The unweighted path length is merely the
number of edges on the path.
53
Shortest-Path Algorithms
Single-Source Shortest-Path Problem:
Given as input a weighted graph G=(V, E), and a
distinguished vertex, s, find the shortest weighted
path from s to every other vertex in G.
54
Edge weights
• Give an example of a graph problem that
needs edge weights.
• What would negative edge weights mean?
55
• In the graph, the shortest path from v1 to v6 has a cost of 6 and the
path itself is v1v4v7v6. The shortest unweighted path has 2 edges.
56
Negative Cost Cycles
• In the graph, we have a negative cost. The path from v5 to v4 has
cost 1, but a shorter path exists by following the loop v5v4v2v5v4
which has cost -5. This path is still not the shortest, because we
could stay in the loop arbitrarily long.
57
Shortest Path Length: Problems
We will examine 4 algorithms to solve four versions of
the problem
1. Unweighted shortest path
2. Weighted shortest path without negative edges
3. Weighted shortest path with negative
4. Weighted shortest path of acyclic graphs
58
Unweighted Shortest Paths
• Using some vertex, s, which is an input parameter,
find the shortest path from s to all other vertices in an
unweighted graph. Assume s=v3. Ideas?
59
Unweighted Shortest Paths
• Algorithm: find vertices that are at distance
1, 2, ... N-1 by processing vertices in layers
(breadth-first search)
60
Queue contains pairs: (node, distance from source)
void PrintShortestLength( int source)
Queue q;
q.enqueue(Pair(source,0));
while(!q.isEmpty())
{ Pair p = dequeue();
if (!V[p.node].isVisited)
{V[p.node].isVisited = true;
cout << “distance from “ << source << “ to “ << p.node << “ is “ <<p.dist;
// get Successors
for (Edge * e = V[p.node].adj; e!= NULL; e = e->next)
p.enqueue( Pair(e->toNode, p.dist+1));
} // if
} // PrintShortestLength
•
61
• What if you wanted the actual path printed?
62
What if the edges had weights?
Weighted Shortest Path Dijkstra’s
Algorithm
• Add the edgewt from node to its successor to the
current distance
• Pull nodes from collection based on shortest distance
– using a priority queue
63
1 function Dijkstra(Graph, source):
2 for each vertex v in Graph:
// Initializations
3
dist[v] := infinity
// Unknown distance function
4
previous[v] := undefined
// stores the best predecessor
5 dist[source] := 0
// Distance from s to s
6 Q := copy(Graph)
// Set of all unvisited vertices
7 while Q is not empty:
// The main loop
8
node := extract_min(Q)
// Remove best vertex from PQ
9
for each neighbor succ of node:
10
newDist = dist[node] + length(node, succ)
11
if newDist < dist[succ]
// Relax (node,succ)
12
dist[succ] := newDist
// Need decrease key?
13
previous[succ] := node
64
Weighted Shortest Path Dijkstra’s
Algorithm
• With weighted shortest path, the initial distance dv
is tentative. It turns out to be the shortest path
length from s to v using only known vertices as
intermediates.
• Greedy algorithm: proceeds in stages doing the
best at each stage.
• Dijkstra’s algorithm selects a vertex v with
smallest dv among all unknown vertices and
declares it known. Remainder of the stage consists
of updating the values dw for all edges (v, w).
65
66
Graphs with negative edge
costs
• Dijkstra’s algorithm does not work with
negative edge costs. Once a vertex u is
known, it is possible that from some other
unknown vertex v, there is a path back to u
that is very negative.
• Algorithm:. Forget about the concept of
known vertices.
67
Graphs with negative edge
costs
• Dijkstra’s algorithm does not work with
negative edge costs.
• Idea – What if we added a large enough
constant to every edge weight so that no
edge is negative?
68
Graphs with negative edge
costs
• Dijkstra’s algorithm does not work with
negative edge costs.
• Idea – What if we added a large enough
constant to every edge weight so that no
edge is negative? Note, this doesn’t work as
now the cost of a path depends on number
of edges of path.
69
What to do?
• We have to ignore the idea of a “known” vertex – since
previously known vertices may get a smaller value. We
put each node we need to consider on a queue. We begin
by placing the start vertex on the queue. At each stage, we
remove a node from the queue. We examine the length to
all successors. For any successors whose distance has
decreased, we change the distance and place that node on
the queue. We stop when all nodes have been removed.
If we have negative cycles, we will be in an infinite loop –
so we need to check for that.
•
70
Graphs with negative edge costs - I
• O(|E|*|V|)
• Each vertex can dequeue
at most O(|V|) times.
(Why? Algorithm
computes shortest paths
with at most 0, 1, ..., |V|1 edges in this order).
Hence, the result!
• If negative cost cycles,
then each vertex should
be checked to have been
dequeued at most |V|
times.
71
Acyclic Graphs
• If the graph is known to be acyclic, the order
in which vertices are declared known, can be
set to be the topological order.
• Running time = O(|V|+|E|)
• This selection rule works because when a
vertex is selected, its distance can no longer be
lowered, since by topological ordering rule it
has no incoming edges emanating from
unknown nodes.
72
What if we need all pairs shortest
path?
• We could use single-source shortest path
repeatedly, but is there a better way?
73
The weight matrix and the
graph
1
1
1
2
3
4
5
0
1
1
5
2
9
0
3
2
3
0
4
4
2
0
3
5
3
4
0
v1
3
5
v5
3
v2
9
1
2
v4
2
4
Copy original matrix. Add new transitive edges
Floyd Warshall Animation
74
3
v3
The subproblems
• How can we define the shortest distance di,j
in terms of “smaller” problems?
• One way is to restrict the paths to only
include vertices from a restricted subset.
• Initially, the subset is empty.
• Then, it is incrementally increased until it
includes all the vertices.
75
• The basic algorithm for all pairs shortest path is as
follows:
Floyd Warshall Algorithm
for k = 1 to v
for i = 1 to v
if path[i,k] != infinity // no path
for j = 1 to v
path[i,j] = min(path[i,j], path[i,k]+path[k,j])
• For me, it is obvious that the algorithm never finds bad
path lengths – as it merely replaces the current length
from i to j with a path through another node (k) if it is
76
shorter.
The weight matrix and the
graph
1
1
1
2
3
4
5
0
1
1
5
2
9
0
3
2
3
0
4
4
2
0
3
5
3
0
77
v1
3
5
v5
3
v2
9
1
2
v4
2
4
3
v3
Example
W=
1
4
5
2
2
D0 =
2
4
3
5
2
0
-3
0
1
0
0
0
2
0
0
0
3
0
0
0
3
-3
P=
P is previous node to use
78
1
2
3
1
0
1
2
3
1
4
5
2
-3
2
79
3
1
0
2
1
0
2
1
2
3
2
4
0
-3
3
5
0
k=1
Does anyone want to go
through node 1?
2
4
0
-3
3
5
7
0
D1[2,3] = min( D0[2,3], D0[2,1]+D0[1,3] )
= min (, 7)
=7
1
2
1
0
0
2
0
0
3
0
1
D1[3,2] = min( D0[3,2], D0[3,1]+D0[1,2] )
= min (-3,)
= -3
3
0
0
0
1
1
D =
2
3
P=
D0 =
1
4
2
-3
2
D1 = 1
5
3
2
3
7
1
0
2
1
2
D =
2
3
1
0
2
-1
2
4
0
-3
3
5
7
0
1
2
1
0
0
2
0
0
3
0
1
3
2
0
0
P=
80
2
4
0
-3
3
5
7
0
k=2
Vertices 1, 2 can be
intermediate
D2[1,3] = min( D1[1,3], D1[1,2]+D1[2,3] )
= min (5, 4+7)
=5
D2[3,1] = min( D1[3,1], D1[3,2]+D1[2,1] )
= min (, -3+2)
= -1
1
4
2
2
D3 =
P=
81
D2 = 1
1 0
3
2
2 2
-3
3 -1
7
2
4
0
-3
5 -1
1
2
3
1
0
2
-1
2
2
0
-3
3
5
7
0
1
2
1
0
0
2
3
0
3
0
1
3
2
0
0
3
5
7
0
k=3
Vertices 1, 2, 3
can be
intermediate
D3[1,2] = min(D2[1,2], D2[1,3]+D2[3,2] )
= min (4, 5+(-3))
=2
D3[2,1] = min(D2[2,1], D2[2,3]+D2[3,1] )
= min (2, 7+ (-1))
=2
Printing intermediate nodes on
shortest path from q to r
interPath(index q, r)
if (P[ q, r ]!=0)
interPath (q, P[q, r])
println(P[q, r])
interPath (P[q, r], r)
return;
//no intermediate nodes
else return
Print q
Print interPath(q,r)
Print r
82
1
P= 2
3
1
4
2
1
0
0
2
2
3
0
0
5
2
-3
3
3
0
1
0
Example
83
The final distance matrix and P
The values in parenthesis are the non zero P values.
84
The call tree for Path(1, 4)
Path(1, 4)
Path(1, 6)
P(1, 6)=0
Print
v6
Path(6, 3)
Path(6, 4)
Print Path(3, 4)
v3
P(3, 4)=0
P(6, 3)=0
The intermediate nodes on the shortest path from 1 to 4 are v6, v3.
The shortest path is v1, v6, v3, v4.
85
© Copyright 2026 Paperzz