Title: Graphs
1Graphs
2Motivating Problem
- Konigsberg bridge problem (1736)
Starting in one land area, is it possible to walk
across all bridges once and return to the initial
land area?
C
d
c
g
A
D
e
a
b
f
The answer is no, but how do we prove that?
B
3Konigsberg Graph Problem
- The Konigsberg is an instance of a graph problem
- Definition of a graph
- Graph G Consists of two sets, V and E
- V A finite, non-empty set of vertices
- E A set of pairs of vertices, where the pairs
are called edges.
4Example Graphs
V 0, 1, 2, 3 E (0,1), (0,2), (0,3) (1,2),
(1,3), (2,3)
V 0, 1, 2, 3 E Empty
0
0
1
2
3
1
2
0
3
V 0, 1, 2, 3 E (0,1), (0,2), (1,3) Trees are
a subset of graphs
1
2
3
5Graph Definitions
- Undirected graph Pair of vertices representing
any edge is unordered - (u,v) is the same edge as (v,u)
- Directed graph Each edge is represented by a
directed pair (u,v) - Drawn with an arrow from u to v indicating the
direction of the edge - (u,v) is not the same edge as (v,u)
6Directed vs. Undirected
0
0
0
1
2
1
2
1
2
3
3
3
Graph C Directed Equivalent to A (1,0) is a
valid edge A (0,1) edge also exists
Graph A Undirected (1,0) is a valid edge Same as
(0,1) edge
Graph B Directed Not equivalent to A (1,0) not
valid edge
7Graph Restrictions
- For now lets assume vertices and edges are sets
- No self edges (vertice back to itself)
- No repeated edges (multigraph)
0
Self
1
2
Repeated
3
8Motivating Problem Graph Restrictions
- Konigsberg bridge problem (1736)
This is the appropriate graph representation. We
re not going to solve it for now because of our
assumption of no repeated edges.
0
1
3
2
9Graph Definitions
- Maximum possible number of distinct unordered
pairs (u,v) (undirected graph) in a graph with n
vertices is n(n-1) / 2. - A graph with this many edges is called a complete
graph.
0
0
Complete 6 edges (4 3) / 2
1
2
1
2
Not Complete (43)/2 ! 4 edges
3
3
10Graph Definitions
- Directed Graph
- Maximum of (n (n-1)) edges. Twice that for
undirected because 2 directed are equivalent to
one undirected - Proof of (n (n 1)) bounds
- n nodes, can point to every other node except
for themselves - n-1 edges connecting to each of the n nodes
11Graph Definitions
- If (u, v) is an edge in E(G),
- Vertices u and v are called adjacent
- The edge (u,v) is called incident on vertices u
and v. - Examples
Vertex 0 is adjacent to 1 and 2 Vertex 1 is
adjacent to 0, 2, and 3 Vertex 2 is adjacent to 0
and 1 Vertex 3 is adjacent to 1 Edges incident
on vertex 2 (0,2), (1,2) Edges incident on
vertex 3 (1,3)
0
1
2
3
12Graph Definitions
- A subgraph of graph G called G is a graph such
that V(G) is a subset V(G) and E(G) is a
subset of E(G)
Subgraphs
0
1
0
1
2
1
2
3
0
3
1
2
13Graph Definitions
- A path from vertex u to vertex v in a graph G is
a sequence of vertices u, i1, i2, , ik, v, such
that (u,i1), (i1,i2)(ik,v) are edges in G. - If in a directed graph, the edges have to be in
the right direction. - The length of a path is the number of edges on
the path. - A simple path is a path in which all vertices
except possibly the first and last are distinct.
14Graph Definitions
Paths from 1 to 3 (1,3) 1,3
Simple Length 1 (1,2), (2,3) 1,2,3
Simple Length 2 (1,0),(0,2),(2,1),(1,3) 1,0,2
,1,3 Length 4 (1,2),(2,0),(0,1),(1,3) 1,2,0,1,3
Length 4 (1,0),(0,2),(2,3) 1,0,2,3
Simple Length 3 Many more that repeat
internally, Not simple, Length gt 4
0
1
2
3
15Graph Definitions
- A cycle is a simple path where the first and last
vertices are the same.
0
Cycles to 1 1,0,2,3,1 1,0,2,1 1,3,2,1 1,2,1 1,0,1
1,3,1
1
2
3
16Graph Definitions
- Two vertices u and v are connected if there is a
path in G from u to v. - An undirected graph is said to be connected (at
the graph level) if and only if for every pair of
distinct vertices u and v in V(G) there is a path
from u to v in G. - A connected component of a graph is a maximal
connected subgraph
17Graph Definitions
0
4
1
2
5
6
3
7
Graph G4 V(G4) 0,1,2,3,4,5,6,7 E(G4)
(0,1), (0,2), (1,3), (2,3), (4,5), (5,6),
(6,7) There are two connected components of G4
H1 (0-3) and H2 (4-7) Verify that H1 and H2
components are connected Path between all pairs
of vertices Directed graphs different because
paths are directed, harder to get connected
components
18Graph Definitions
- A tree is a connected, acyclic graph
- For any node there is path to any other node
(usually back through a parent node) - Acyclic property forces a unique path between
nodes
0
1
4
3
2
19Graph Definitions
- Need a corollary to connected for directed
graphs - A directed graph G is strongly connected if for
every pair of distinct vertices in V(G), u and v,
there is a directed path from u to v and from v
to u. - A strongly connected component is a maximal
subgraph of a directed graph that is strongly
connected
20Graph Definitions
0
1
2
Graph G5 V(G5) 0,1,2 E(G5) (0,1),
(1,0), (1,2) G5 is not strongly connected (No
path from 2 to 1) There are two strongly
connected components of G4 H1 (0-1) and H2
(2) Verify that H1 and H2 components are strongly
connected Directed path between all pairs of
vertices
21Graph Definitions
- The degree of a vertex v is the number of edges
incident to that vertex. - For a directed graph,
- The in-degree of a vertex v is the number of
edges for which the arrow points at v. - The out-degree is defined as the number of edges
for which the arrow points away from v.
22Graph Definitions
0
Degree of Vertices 0, 3 gt Degree 2 1, 2 gt
Degree 3
1
2
3
0
0 Out-Degree 1 In-Degree 0 1 Out-Degree 1
In-Degree 0 2 Out-Degree 0 In-Degree 2
1
2
23Graph Definitions
- For undirected graph whose vertices v_i have
degree d_i, the number of edges, E, is (the sum
from 0 to n-1 of degree_i ) / 2 - Essentially just counting the edges.
- Divide by 2 because double counting (if node1,
node2 share an edge, that edge is in both of
their degrees) - Useful for computing max number of edges if you
only know number of vertices and their degree (ie
they are all binary gt E 2 V / 2 V) - Given our graph assumptions (no repeated edges,
no self edges) degree has to be lt V-1
24Graph Definitions
0
Degree of Vertices 0, 3 gt Degree 2 1, 2 gt
Degree 3 E Sum of Degrees / 2 (2233)/2
10/2 5 Correct!
1
2
3
0
E max Sum of Degrees / 2 (2 2 2) / 2
6/2 3 Correct!
E Max ?, All Binary
1
2
25(No Transcript)
26Graph Representations
- What core functionality do we need in
representation? - Set of vertices
- Set of edges
- Two major representations
- Adjacency matrix Array based
- Adjacency list Linked List based
27Adjacency Matrix
- G (V,E) graph with V n, n gt 1
- Adjacency matrix
- 2 dimensional n x n array called A with property
that Aij 1 if the edge (i,j) is in E, 0
otherwise.
V0 V1 V2 V3
V0 0 1 1 0
V1 1 0 0 1
V2 1 0 0 1
V3 0 1 1 0
0
1
2
3
28Adjacency Matrix
- Directed Graphs Rows are the out indicators
(data in a row indicates that there is an
outgoing link)
V0 V1 V2 V3
V0 0 0 1 0
V1 1 0 0 1
V2 0 0 0 0
V3 0 0 1 0
0
1
2
3
29Adjacency Matrix
- Note how the adjacency matrix for an undirected
graph is symmetric. Can save approximately half
the space by storing only upper triangle or lower
triangle
0
V0 V1 V2 V3
V0 0 1 1 0
V1 1 0 0 1
V2 1 0 0 1
V3 0 1 1 0
1
2
3
30Adjacency Matrix
Given a complete adjacency matrix, can
easily Determine if there is an edge between
any two vertices (look in appropriate
column) Undirected Graph Compute degree of a
node (sum over row)
0
V0 V1 V2 V3
V0 0 1 1 0
V1 1 0 0 1
V2 1 0 0 1
V3 0 1 1 0
1
2
3
31Adjacency Matrix
Given a complete adjacency matrix, can easily
Directed Graph Compute out degree of a node
(sum over row) Compute in
degree of a node (sum over column)
V0 V1 V2 V3
V0 0 0 1 0
V1 1 0 0 1
V2 0 0 0 0
V3 0 0 1 0
0
1
2
3
32Adjacency Matrix
- What if we want to compute a non-trivial answer?
- How many total edges are there in the graph?
- Is the graph connected?
- Total edges Requires O(n2) operations
- (n2 entries n diagonals always 0)
0
V0 V1 V2 V3
V0 0 1 1 0
V1 1 0 0 1
V2 1 0 0 1
V3 0 1 1 0
1
2
3
33Adjacency Matrix
- What if we have a sparse graph?
- Sparse Very few connections out of all possible
V0 V1 V2 V3 V4 V5 V6 V7
V0 0 1 1 0 0 0 0 0
V1 1 0 0 1 0 0 0 0
V2 1 0 0 1 0 0 0 0
V3 0 1 1 0 0 0 0 0
V4 0 0 0 0 0 1 0 0
V5 0 0 0 0 1 0 1 0
V6 0 0 0 0 0 1 0 1
V7 0 0 0 0 0 0 1 0
4
0
5
6
1
2
7
3
34Adjacency Matrix
- Would really like to do O(E) operations when
counting edges - O(n2) is a given when using adjacency matrix
- For dense graphs, E is close to n2
- Not for sparse graphs (E ltlt n2)
- Solution Use linked lists and store only those
edges that are really represented in the graph
(no 0s for things that arent present). - Slightly more complicated to implement but saves
a lot of time
35Adjacency List
- N rows of adjacency matrix (vertices) are
represented as n linked lists. - Nodes in list i are those nodes adjacent to the
corresponding vertex v_i.
Array of Head Node Pointers
0
0
1
2
1
3
0
1
2
2
0
3
3
1
2
3
Order in linked list doesnt matter
36Adjacency List
- Undirected Graph n vertices, e edges Requires
n head nodes, 2 e list nodes - For any vertex, computing degree (number of
incident edges) is counting size of corresponding
list. - Number of edges for whole graph is computed in
O(n e) ltlt O(n2)
37Adjacency List for Directed Graph
- N rows of adjacency matrix (vertices) are
represented as n linked lists. - Nodes in list i are those nodes that one can
reach from leaving the corresponding vertex
Array of Head Node Pointers
0
0
2
1
2
1
3
0
2
3
2
3
Order in linked list doesnt matter
38Adjacency List
- For a directed graph, nodes in a list are those
that you can reach leaving from the corresponding
vertex - Computing out degree for any vertex Count
number of nodes in corresponding list - Computing number of total edges in graph Adding
all outdegrees gt O(n e) visit all head nodes
and all items in lists ltlt O(n2)
39Adjacency List
- For directed graphs, this approach isnt very
useful for determining in-degree - This was trivial in an adjacency matrix (sum down
a column) - You can build the inverse adjacency list at the
same time building adjacency list.
Array of Head Node Pointers
0
0
1
1
2
1
2
0
3
3
1
3
40Primitive Graph Operations
- In essence, can just think of a graph as a
container, holding edges and vertices (with a lot
of special properties) - class Graph
-
- public
- Graph() // create an empty graph
- void InsertVertex(Vertex v) // Insert v into
graph with no incident edges - void InsertEdge(Vertex u, Vertex v) // Insert
edge (u,v) into graph - void DeleteVertex(Vertex v) // Delete v and
all edges incident to it - void DeleteEdge(Vertex u, Vertex v) Delete
edge (u,v) from graph - bool IsEmpty() // if graph has no vertices
return true - ListltVertexgt Adjacent(Vertex v) // return list
of all vertices adjacent to vertex v -
41Adjacency Matrix/List Construction
- Note we will be updating these data structures as
we call our graph class methods - InsertVertex(Vertex v)
- InsertEdge(Vertex u, Vertex v)
- DeleteVertex(Vertex v)
- DeleteEdge(Vertex u, Vertex v)
- Linked list approach likely more useful over
arrays if dont know beforehand what adding to
graph (how many edges, etc).
42Weighted Edges
- Often see weighted edges on graphs
- Common uses
- Distance between vertices
- Cost of moving from one vertex to another vertex
- Think of vertices as cities could be real
distances or flight costs and want to know
shortest/cheapest path - How do we incorporate these weights into our
graph representations?
43Weighted Edges
Could store weights in adjacency matrix (just
need a non-zero entry) 0 1 2 3 0
0 60 40 55 1 60 0 0 0 2 40
0 0 50 3 55 0 50 0 If using
lists, add another field to node
Mehran
Ilam
60
0
1
40
Sarableh
2
55
50
3
Ivan
44Elementary Graph Operations
- First Operation Traversal of graphs
- Already saw how this worked in binary trees
(inorder, preorder, postorder, depth-order) - Similar idea for general graphs
- Given a graph G (V,E) and a vertex v in V(G),
visit all vertices in G that are reachable from v
- This is the subset of the graph that is connected
to v.
45Graph Traversal
- Depth-First Search
- Similar to descending down binary tree
- Basic algorithm
- Begin by starting at vertex v.
- Select an edge from v, say its the edge (v,w).
- Move to vertex w and recurse.
- When a node has been visited that has all of its
adjacent vertices visited, back up to the last
node with an unvisited adjacent vertex and
recurse.
46DFS Traversal
- void GraphDFS() // driver
-
- visited new booln
- for (int i 0 i lt n i) visitedi false
- DFS(0)
- delete visited
-
- void GraphDFS(const int v) // workhorse
-
- visitedv true
- for (each vertex w adjacent to v) //use
adjacency matrix or lists - if (!visitedw) DFS(w)
47DFS Traversal Graph To Traverse
0
1
2
1
0
3
4
0
2
0
5
6
1
2
3
1
7
4
1
7
3
4
5
6
5
2
7
6
2
7
7
7
3
4
5
6
48DFS Traversal
Order of Traversal 0, 1, 3 7, 4, 5, 2, 6 Note
that performing DFS can find connected
components. In this case, the whole graph is
connected and thus all nodes were visited.
0
1
2
3
4
5
6
7
49Analysis of DFS From a single node
- Running time dependent on graph structure and
representation - If graph G is represented by adjacency lists
- Determine vertices adjacent to vertex v by
following chain of links. - Each node in each lists is visited at most once,
and there are 2e list nodes. Thus, the running
time is bounded by the number of edges. - If graph G is represented by an adjacency matrix
- Have to look at n items (n number of vertices)
for each vertex (scanning down each row in the
matrix) - Running time is O(nn).
50Breadth First Traversal
- Similar to descending across levels of a binary
tree. - Visit the starting vertex v.
- Visit all unvisited vertices directly adjacent to
v. - Recurse, visiting all unvisited vertices directly
adjacent to those from the previous step.
51BFS Traversal Algorithm
- void GraphBFS(int v)
-
- visited new booln
- for (int i 0 i lt n i) visitedi false
- visitedv true
- Queueltintgt q
- q.insert(v)
- while (!q.isEmpty())
- v q.Delete(v)
- for (all vertices w adjacent to v)
- if (!visitedw)
- q.insert(w)
- visitedw true
-
-
- delete visited
52BFS Traversal Graph to Traverse
0
1
2
1
0
3
4
0
2
0
5
6
1
2
3
1
7
4
1
7
3
4
5
6
5
2
7
6
2
7
7
7
3
4
5
6
53BFS Traversal
Order of Traversal 0, 1, 2, 3, 4, 5, 6, 7 Note
that performing BFS can find connected
components. In this case, the whole graph is
connected and thus all nodes were visited.
0
1
2
3
4
5
6
7
54Analysis of BFS
- Each visited vertex enters queue exactly once (n
vertices). - Once in the queue, have to review list of
neighbors. - For adjacency matrix, that list is n items long,
meaning the total time is O(nn) - For adjacency list, that list has degree(vertex)
items, and the sum of the degrees for all n
vertices is O(e), so total cost is bounded by
number of edges - Same cost as DFS
55Connected Components
- How do we find all connected components?
- Calling BFS or DFS will find a connected
component - Those vertices connected to the start node
- To find all connected components,
- Select a start vertex, call DFS
- Select another start vertex which hasnt been
visited by a previous DFS, call DFS - Repeat until all vertices have been visited.
56Connected Components Algorithm
- void GraphComponents()
-
- visited new booln
- for (int i 0 i lt n i) visited false
- for (i 0 i lt n i)
-
- if (!visitedi) DFS(i) outputComponent()
-
- delete visited
57Analysis of Connected Components
- Using adjacency lists,
- Any call to DFS is O(e) where e is the set of
edges present in the particular connect component
the start node is in. - Sum over e has to equal E, the total number of
edges - For loop itself takes O(n) time and calls DFS for
non-visited nodes - DFS not called every time as mark many nodes with
each DFS - Total time O(ne)
- With adjacency matrix, O(nn) have to look at
all columns for all rows
58Spanning Trees
- If a graph G is connected, a DFS or BFS starting
at any vertex visits all vertices in G. - There are a set of particular edges that are
traversed during this process. - Let T be the set of edges that are traversed, and
N be the remaining edges.
59BFS Traversal
Order of Traversal 0, 1, 2, 3, 4, 5, 6,
7 Edges traversed T (0,1), (0,2), (1,3),
(1,4), (2,5), (2,6) (3,7) Not traversedN (4,7)
, (5,7), (6,7)
0
1
2
3
4
5
6
7
60A Spanning Tree Algorithm
- Not hard to record traversed edges in BFS (or
DFS) add to a dynamic list whenever encounter
new edge. - void GraphBFS(int v)
-
- visited new booln
- LinkedList tEdgeList new LinkedList()
- for (int i 0 i lt n i) visitedi false
- visitedv true
- Queueltintgt q
- q.insert(v)
- while (!q.isEmpty())
- v q.Delete(v)
- for (all vertices w adjacent to v)
- if (!visitedw)
- q.insert(w)
- tEdgeList.append(v,w)
- visitedw true
- delete visited
61Spanning Trees
- This set of edges is called a spanning tree for
the graph. - A spanning tree is any tree that consists solely
of edges from G and that includes all vertices in
G (the tree spans all vertices of the graph). - Spanning trees are not unique.
- Generated from DFS called depth-first spanning
tree - Generated from BFS called breadth-first
spanning tree
62BFS Spanning Tree
0
Order of Traversal 0, 1, 2, 3, 4, 5, 6, 7
1
2
0
3
4
5
6
1
2
7
3
4
5
6
Edges traversed T (0,1), (0,2), (1,3), (1,4),
(2,5), (2,6) (3,7)
7
BFS Spanning Tree
63DFS Spanning Tree
0
Order of Traversal 0, 1, 3 7, 4, 5, 2, 6
0
1
2
1
2
3
4
5
6
3
4
5
6
7
Edges traversed T (0,1), (1,3), (3,7), (7,4),
(7,5), (5,2), (2,6)
7
DFS Spanning Tree
64Spanning Trees
- Spanning tree is a minimal subgraph G of G such
that V(G) V(G) and G is connected - Given all vertices from original graph, it is the
smallest set of edges one needs for the graph to
be connected (to be able to get from any one node
to any other) - Any connected graph with n vertices must have n-1
edges or more. - Minimally, every vertex has incoming and outgoing
(2n) except last and first (-2) gt 2n-2. Every
edge is counted twice (as its outgoing and
incoming) so divide by 2 gt (2n-2)/2 n-1 - Thus, a spanning tree has exactly n-1 edges (it
is the minimal subgraph of G that is connected).
65Applications of Spanning Trees
- Very useful in determining optimal connections
- Example Communication networks (laying out
cable) Assume vertices are cities and edges are
communication links between cities. - All possible spanning trees are all the possible
ways you could set up communication links so that
everyone could talk to everyone else. - Usually interested in finding the cheapest set of
links Requires setting weights (costs) on links
and finding minimum spanning tree will see
algorithm soon! - Interesting problem there are potentially many
spanning trees? How do you find the minimal
efficiently (cant look at them all!)
66Finding Minimum CostSpanning Trees
- Given a weighted directed graph and a spanning
tree for the graph, define the cost of the
spanning tree as the sum of the weights of the
trees edges. - The minimum cost spanning tree is the tree with
minimal cost (the smallest sum over edge
weights). - 3 different efficient algorithms for finding
minimum spanning tree
67Greedy Algorithms
- All 3 algorithms are greedy
- Work in stages
- From the set of feasible decisions, make the best
decision possible (given some metric of best) for
current stage. - Cant change mind later
- Feasible ensures that the solution will obey
problem constraints - Repeat for rest of stages, given the decisions
you have already made and whats left to do. - Best is usually defined as least cost or highest
profit. - Useful in many other programming domains