05 Graphs (1)
05 Graphs (1)
Graphs
SPRING 2025
1
Outline
Introduction to graphs
• Examples of graphs in real-life
• Graph representation for computer algorithms
Weighted graphs
• Weighted graphs representation for computer algorithms
2
Introduction to Graphs
A graph is informally thought of as a collection of points in
the plane called vertices or nodes.
“vertices” or “nodes” are connected by line segments called
edges.
G= (V,E)
V: a finite non-empty set V of items called vertices.
E: a set E of pairs of these items called edges.
3
Introduction to Graphs
UNDIRECTED GRAPHS DIRECTED GRAPHS
V = {a,b,c} V = {a,b,c}
and edge set
and edge set
E={(a,b), (a,c), (b,c), (c,a)}
E = {(a,c), (a,b),(b,c)}
a a
b c b c
4
Examples of graphs in real-life
Edges represent
routes and vertices
represent buildings.
5
Examples of graphs in real-life
Edges represent
routes and vertices
represent cities.
6
Graph representations
Graph for computer algorithms are usually represented in
one of two ways:
Adjacency matrix
Adjacency lists
7
Graph representations
Adjacency Matrix
The adjacency matrix of graph with n vertices is an n x n Boolean
matrix
With one row and one column for each of the graph’s vertices, in which:
the element in the ith row and the jth column is equal to 1 if there is an edge
from the ith vertex to the jth vertex
and equal to 0 if there is no edge.
8
Adjacency Matrix
For undirected graphs, the adjacency matrix is symmetric.
Directed graphs have asymmetric adjacency matrices.
9
Powers of Adjacency Matrix
Let 𝑨𝑨 be an adjacency matrix.
Then, an entry 𝑎𝑎𝑖𝑖𝑖𝑖 of 𝐴𝐴𝑛𝑛 can be interpreted as the number of
walks from node 𝑖𝑖 to node 𝑗𝑗 of length 𝑛𝑛.
0 1 0 0 12 2 1 1 2 1
1 0 1 1 1 1 4 1 2 2
0 1 0 1 0 = 1 1 2 1 2
0 1 1 0 1 2 2 1 3 1
1 1 0 1 0 1 1 2 1 3
10
Powers of Adjacency Matrix
Applications.
Is the graph connected?
0 1 0 0 12 2 1 1 2 1
1 0 1 1 1 1 4 1 2 2
0 1 0 1 0 = 1 1 2 1 2
0 1 1 0 1 2 2 1 3 1
1 1 0 1 0 1 1 2 1 3
11
Powers of Adjacency Matrix
Applications.
How many triangles does the below graph have?
0 1 0 0 13 2 6 3 3 5
1 0 1 1 1 6 6 6 7 7
0 1 0 1 0 = 3 6 2 5 3
0 1 1 0 1 3 7 5 4 7
1 1 0 1 0 5 7 3 7 4
12
Powers of Adjacency Matrix
Applications.
How many triangles does the below graph have?
2 6 3 3 5
6 6 6 7 7
trace 3 6 2 5 3 = 2 + 6 + 2 + 4 + 4 = 18
3 7 5 4 7
5 7 3 7 4
13
Exercise (at home)
How would you compute the number of
quadrilaterals (4-gons) in a graph?
14
Variations
Weight matrix:
Store weights in matrix, set diagonal to 0.
Laplacian matrix:
Store degree in diagonal, use -1 to indicate adjacent nodes.
15
Graph representations
Adjacency lists
Adjacency lists of a graph is a collection of linked list, one for each
vertex.
The linked list contains all the vertices adjacent to the list’s vertex (i.e.,
all the vertices connected to it by an edge).
Usually, such lists start with a header identifying a vertex for which the
list is compiled.
16
Trade-offs.
Testing whether there is an edge from v to u:
Adjacency matrix: O(1)
Edge list: O(deg(v))
17
Graph representations
Undirected graph
Directed graph
18
Weighted graph
A weighted graph is a graph with numbers assigned to its edges.
These numbers are called weights or costs.
An interest in such a graph is motivated by numerous real-world
applications
The weight in a real-world application represent:
The physical distance between two vertices
The time it takes to get from one vertex to another
The costs to travel from vertex to vertex.
19
Weighted graph
representations
Weight matrix or cost matrix
If a weighted graph is represented by its adjacency matrix then:
its element A[i,j] will simply contain the weight of the edge from the ith to
the jth vertex if there us such an edge
and a special symbol, e.g., ∞ (or 0 in some books), if there is no such edge.
20
Weighted graph
representations
Adjacency lists
Adjacency lists for a weighted graph have to include in their nodes not
only the name of an adjacent vertex but also the weight of the
corresponding edge.
21
More terminologies to learn..
A graph with every pair of its vertices connected by an edge is called
complete
𝑵𝑵(𝑵𝑵−𝟏𝟏)
An undirected complete graph has edges
𝟐𝟐
22
More terminologies to learn..
23
More terminologies to learn..
24
More terminologies to learn..
25
More terminologies to learn..
Paths and Cycles
A path from vertex u to vertex v of a graph G can be defined as a
sequence of adjacent vertices (connected by an edge) that starts with u
and ends with v.
If all vertices of a path are distinct, the path is said to be simple.
The length of a path is the total number of vertices in the vertex
sequence defining the path minus 1, which is the same as the number
of edges in the path.
Simple path:
a,c,b,f of length 3
26
Paths and Cycles
Directed Path
A directed path is a sequence of vertices in which every consecutive
pair of the vertices is connected by an edge directed from the vertex
listed first to the vertex listed next.
Directed path:
a,c,e,f
27
Paths and Cycles
Connected Graph
A graph is connected if for every pair of its vertices u and v there is a
path from u to v.
Connected component
If a graph is not connected, the graph will consist of several connected
pieces that are called connected components of the graph.
28
Paths and Cycles
A cycle is a path of length 1 or more that starts and ends at the same
vertex and does not traverse the same edge twice.
It is important to know for many application whether a graph under
consideration has cycles.
Motivation for edges like
A graph with no cycle is said to be acyclic (0,0) on next slide.
There are four cycles in this graph,
could you identify them??
There is a cycle:
(0, 0) of length 1
(0, 2, 0) of length 2
(1, 3, 2, 1) of length 3
(1, 3, 2, 4, 1) of length 4.
29
(Finite) State MachineExample
31
Some fun applications.
Computational Graphs store arithmetic operations.
They are used in Deep Learning (back-propagation) and
visualization (modeling of adjacent segments).
32
Exercise
Create the Adjacency Matrix and Adjacency lists for the
following graph:
33
Exercise 1
1 2 3 4 5
1 0 0 0 1 0 1 →4
2 →4→5
2 0 0 0 1 1 3 →5
4 →1 →2 →5
3 0 0 0 0 1 5 → 2 →3 →4
4 1 1 0 0 1
Adjacency List
5 0 1 1 1 0
Adjacency Matrix
34
Exercise
Write down the adjacency list of the following graphs.
How much storage does the adjacency matrix / adjacency
list require?
35
Storage requirements
Adjacency matrix: |V|x|V|, where |V| is the number of vertices,
O(|V|2).
Adjacency list: |V| for the list heads, then all the edges |E|. The worst-
case is a complete graph with |E|=O(|V|2). Thus, O(|V|2) as well.
The truth: for a binary matrix, |V|2/8 bytes (one bit per entry).
Adjacency list: |V|2 log |V|, since we need to store at least one number
between 0 and |V|-1 for each adjacent vertex. Each number (without
further compression) requires log2 |V| bits.
36
Exercise
Draw a graph for the following Adjacency Matrix.
a b c d e f g h
a 0 1 0 0 0 0 0 0
b 0 0 0 1 1 1 0 0
c 0 0 0 0 0 0 1 0
d 1 0 0 0 1 0 0 0
e 0 0 0 0 0 0 0 1
f 0 0 1 0 0 0 0 1
g 0 0 0 0 0 0 0 0
h 0 0 0 0 0 1 1 0
37
Exercise
Draw a graph for the following Adjacency Matrix.
Question: Is this a directed or undirected graph?
a b c d e f g h
a 0 1 0 0 0 0 0 0
b 0 0 0 1 1 1 0 0
c 0 0 0 0 0 0 1 0
d 1 0 0 0 1 0 0 0
e 0 0 0 0 0 0 0 1
f 0 0 1 0 0 0 0 1
g 0 0 0 0 0 0 0 0
h 0 0 0 0 0 1 1 0
38
Exercise 2
Adjacency matrix not
symmetric : directed graph
a b c d e f g h
a 0 1 0 0 0 0 0 0
b 0 0 0 1 1 1 0 0
c 0 0 0 0 0 0 1 0
d 1 0 0 0 1 0 0 0
e 0 0 0 0 0 0 0 1
f 0 0 1 0 0 0 0 1
g 0 0 0 0 0 0 0 0
h 0 0 0 0 0 1 1 0
39
Representing Graph in a
Program
• Vertices
• Adding vertices to a graph
• Adding edges to a graph
• Searches
40
Representing Graph in a
Program
Vertices
In a real-world problem the objects must be described using data items.
If a vertex is representing a city (in an airline company) it may need to
store:
◦ the name of the city
◦ its altitude
◦ its location
◦ and other useful information.
41
Representing Graph in a
Program
Here is what the vertex class looks like:
class vertex {
public:
std::string label; // label (e.g., “A”)
bool visited;
vertex(const std::string& lab) : // constructor
label(lab),
visited(false)
{}
vertex(const vertex& other) : // copy constructor
label(other.label),
visited(other.visited)
{}
}; // end class Vertex
42
Representing Graph in a
Program
Adding vertices to a graph
To add a vertex to a graph, you make a new vertex object
and insert it into your vertex array.
In a real-world program a vertex might contain many data
items, but for simplicity we’ll assume that it contains only a
string.
Thus, the creation of a vertex looks something like this:
vertexList[nVerts++] = new Vertex(“F”); // or
vertexList.push_back(new Vertex(“F”);
43
Representing Graph in a
Program
Adding edges to a graph
How you add an edge to a graph depends on whether you are using an
adjacency matrix or adjacency lists to represent a graph.
Let’s say you are using an adjacency matrix and want to add an edge
between vertices 1 and 3.
These numbers correspond to the array indices in vertexList where vertices
are stored.
When you first created adjacency matrix adjMat, you filled it with 0s. To
insert the edge you enter:
adjMat[1][3] = 1;
Meaning: there is an edge that connects vertexList[1] and vertexList[3]
adjMat[3][1] = 1; // undirected edges result in symmetric matrices
44
The Graph Class
class graph {
public:
graph(int maxVerts=20); // constructor
void addVertex(const std::string& label);
void eddEdge(int from, int to);
void displayVertex(int v);
std::vector<int> findPath(int x, int y);
bool hasPath(int x, int y);
protected:
vertex* vertexList; // vertex list
int * adjMat; // adjacency matrix
int nVerts; // current number of vertices
int& adj(int i, int j); // access adjacency matrix
int nMaxVerts; // maximum number of vertices
};
45
The Graph Class
graph::graph(int maxVert) {
vertexList = new vertex[maxVert];
adjMat = new int[maxVert*maxVert]; // “1D” buffer
nMaxVerts = maxVert;
}
46
Searches
There are two common approaches to searching graphs
o Depth-first search (DFS) – implemented a stack
o Breadth-first search (BFS) – implemented with a queue
47
Depth-first search (DFS)
• Uses a stack to remember where it should go when it reaches a dead end.
3 4
2
B F H
5
1 C
A
6 7 8
D G I
9
E
48
Depth-first search (DFS)
To carry out the depth-first search, you pick a starting point-in this case A.
You then do three things:
1. visit this vertex, push it onto a stack so you can remember it, and mark it
so you won’t visit it again.
Then, apply rule 1.
Rule 1: if possible, visit an adjacent unvisited vertex, mark it and push it on
the stack.
2. If there is no unvisited vertices adjacent to the vertex you are visiting
apply rule 2 (otherwise apply rule 1).
Rule 2: if you can not follow rule 1, then, if possible, pop a vertex off the
stack.
3. Rule 3: if you can not follow rule 1 or rule 2, you are done.
Can you show how the stack looks in the various stages of this process?
49
Exercise
Perform a Depth-First traversal of this graph, starting at A.
B F H
C
A
D G I
50
To carry out the depth-first search, you
pick a starting point-in this case A.
You then do three things:
1. visit this vertex, push it onto a stack
so you can remember it, and mark it so
you won’t visit it again. 3 4
2
Then, apply rule 1. B F H
51
Event Stack
Visit A A
Visit B AB
Visit F ABF
Visit H ABFH
Pop H ABF 3 4
2
Pop F AB B F H
Pop B A 5
1 C
Visit C AC A
7
6 8
Pop C A D G I
Visit D AD
Visit G ADG 9
E
Visit I ADGI
Pop I ADG
Pop G AD
Pop D A
Visit E AE
Pop E A
Pop A
52
Done
Depth-first search (DFS)
You might say that the depth-first search algorithm likes to
get as far away from the starting point as quickly as possible
and returns only when it reaches a dead end.
If we use the term depth to mean the distance from the
starting point, you can see where the name depth-first
search comes from.
53
Depth-first search (DFS): C++
A key to the DFS algorithm is being able to find the
vertices that are unvisited and adjacent to specified
vertex.
The first step is to identify the adjacent unvisited
vertex:
The adjacency matrix is the key by going to the row for
the specified vertex and stepping across the columns.
1. You can pick out the column with a 1 (the column number is
the number of an adjacent vertex).
2. You can then check whether this vertex is unvisited.
54
Depth-first search (DFS): C++
// returns an unvisited vertex adjacent to v
int getAdjUnvisitedVertex(int v) {
for (int j=0; j<nVerts; j++) // nVerts current number of vertices
if (adj(v,j)==1 && !vertexList[j].visited)
return j; //return first such vertex
return -1; //no such vertices
} // end getAdjUnvisitedVertex()
55
Depth-first search (DFS): C++
The second step is to implement dfs() method which carries out the
depth-first search.
The code of this method should embody the three rules listed earlier.
It loops until the stack is empty. Within the loop, it does four things:
1. It examines the vertex at the top of the stack, using peek().
2. It tries to find an unvisited neighbor of this vertex.
3. If it doesn't find one, it pops the stack.
4. If it finds such a vertex, it visits that vertex and pushes it onto the
stack.
56
Depth-first search (DFS): C++
void dfs(void) { // depth-first search, begin at vertex 0
std::stack<int> todo;
vertexList[0].visited = true; // mark it
displayVertex(0); // display it
todo.push(0); // push it
57
Depth-first search (DFS): C++
while ( !todo.empty() ) { // until stack empty, (Rule 3)
// get an unvisited vertex adjacent to stack top
int v = getAdjUnvisitedVertex( todo.top() );
if (v==-1) { // if no such vertex, (Rule 2)
todo.pop();
} else { // if it exists,
vertexList[v].visited = true; // mark it
displayVertex(v); // display it
todo.push(v); // push it (Rule 1)
}
} // end while Continued on next slide
Note: this code is for traversal. For search, check if v is equal to a query/
target vertex in each step. If so, return v, otherwise, return -1 (not found)
after the while loop.
58
Depth-first search (DFS): C++
// stack is empty, so we're done
for (int j=0; j<nVerts; j++) // reset flags
vertexList[j].visited = false;
} // end dfs
59
Breadth-first search (BFS)
We saw in the depth-first search the algorithm acts as
though it wants to get as far away from the starting point as
quickly as possible.
In the breadth-search, the algorithms stay as close as
possible to the starting point.
It visits all the vertices adjacent to the starting vertex, and
only then goes further afield.
This kind of search is implemented using a queue instead of
a stack.
60
Exercise
Perform a Breadth-First traversal of this graph, starting at A
B F H
C
A
D G I
61
Breadth-first search (BFS)
The figure below shows the same graph but here the breadth-
first search is used. The numbers indicate the order in which
the vertices are visited
8
2 6
B F H
3
1 C
A
4 7
6 9
D G I
5
E
62
Breadth-first search (BFS)
Follow the rules below:
Start somewhere, so you visit it and make it your current vertex
Rule 1: visit the next unvisited vertex (if there is one) that’s
adjacent to the current vertex, mark it, and insert it into the
queue.
Rule 2: If you can’t carry out Rule 1 because there are no more
unvisited vertices, remove a vertex from the queue (if possible)
and make it the current vertex.
Rule 3: If you can’t carry out Rule 2 because the queue is empty,
you’re done.
63
Follow the rules below:
Start somewhere, so you visit it
and make it your current vertex
Rule 1: visit the next unvisited
vertex (if there is one) that’s
adjacent to the current vertex,
mark it, and insert it into the
queue.
Rule 2: If you can’t carry out 8
Rule 1 because there are no 2 6
F H
more unvisited vertices, remove B
3
a vertex from the queue (if 1 C
possible) and make it the A
4 7
current vertex.
D G I
9
Rule 3: If you can’t carry out
5
Rule 2 because the queue is E
empty, you’re done.
64
Event Queue (front to Rear)
Visit A
Visit B B
Visit C BC
Visit D BCD
Visit E BCDE
Remove B CDE
Visit F CDEF
Remove C DEF
8
Remove D EF 2 6
B F H
Visit G EFG 3
Remove E FG 1 C
A
4 7
Remove F G 6 9
D G I
Visit H GH
Remove G H 5
E
Visit I HI
Remove H I
Remove I -
Done -
65
Breadth-first search (BFS): C++
The bfs() method of the Graph Class is similar to the dfs()
method, expect that it uses a queue instead of a stack and
features nested loops instead of a single loop.
o The outer loop wait for the queue to be empty (rule 3)
o The inner one looks in turn at each unvisited neighbor of
the current vertex. (rule 1 and 2)
66
Breadth-first search (BFS): C++
void bfs(void) { //breadth-first search, begin at vertex 0
std::deque<int> todo;
vertexList[0].visited = true; //mark it
displayVertex(0); //display it
todo.push_back(0); //insert at tail
int v2;
67
Breadth-first search (BFS): C++
while ( !todo.empty() ) { // until queue empty,
int v1 = todo.front(); // remove vertex at head (Rule 2)
todo.pop_front();
// until it has no unvisited neighbors
while ( (v2=getAdjUnvisitedVertex(v1)) != -1 ) { // get one,
vertexList[v2].wasVisited = true; // mark it (Rule 1)
displayVertex(v2); // display it
todo.insert(v2); // insert it
} // end while
} // end while(queue not empty) Rule 3
Note: this code is for traversal. For search, check if v2 is equal to a
query/ target vertex in each step. If so, return v2, otherwise, return -1
(not found) after the outer while loop.
68
Breadth-first search (BFS): C++
// queue is empty, so we're done
for (int j=0; j<nVerts; j++) // reset flags
vertexList[j].visited = false;
} // end bfs()
69
Challenging Graph Problems
Travelling Salesperson:
• Nodes represent cities or clients
• Edges represent a travel from A to B
• Weights on edges: cost to travel from A to B
• Task: Find the route that visits all cities or clients with minimal cost
• Complexity: O(N!) (exponential)
• Efficient approximations exist for the metric problem
(metric: edge weights satisfy triangle inequality, a tour A-B-C costs at
least as much as A-C)
70
Challenging Graph Problems
Hamiltonian Path & Hamiltonian Cycle:
• Does the graph contain a path/cycle that visits each vertex exactly
once?
• Equivalent to TSP with adjacent city distances of 1 and 2 otherwise
• Does the shortest TSP path have a length of n?
• Complexity: NP-complete
71
Challenging Graph Problems
Complete Coloring (Achromatic number):
• Given M colors, choose one color for each vertex
• Each pair of colors needs to be used on adjacent vertices at least once
• What is the minimum number of colors for a given graph?
• Complexity: NP-hard (exact achromatic number)
• Complexity: NP-complete (achromatic number larger than given K?)
72
Challenging Graph Problems
Clique Problem:
• Does the graph contain a clique larger than N?
• Find the clique with the maximum size
• Complexity: NP-complete (exponential)
73
NP? NP-complete?
P/PTIME Problem: NP Problem:
(non-deterministic polynomial time)
𝑂𝑂 𝑛𝑛𝑝𝑝 𝑂𝑂 𝑛𝑛𝑝𝑝
“Quickly verifiable”
“Quickly solvable”
74
An NP example
Sudoku.
N = k4 grid cells (each cell has k2 numbers)
Verification: Check the board,
• do rows contain 1..k2? – O(N)
• do columns contain 1..k2? – O(N)
• do cells contain 1..k2? – O(N)
“Quickly verifiable”.
75
An NP example
Sudoku.
Solving complex Sudokus:
All known algorithms grow exponentially in N.
76
Another NP example
Cryptography (John Nash, 1955).
77
NP Completeness
Given a problem P1, assume you were to find a quick way of
solving P1.
P1 is NP-complete if:
Any NP-complete problem P2 can be transformed in polynomial
time to an input of P1
and
P1’s output can be turned in polynomial time to a solution of Ps.
78
Additional Complexity Classes
EXP / EXPTIME: Intractable
79
Example: Subset Sum
Given a list of integers M (positive and negative), is there a
subset L ⊆ M such that
∑𝑙𝑙∈𝐿𝐿 𝑙𝑙 = 0?
80
Example: Subset Sum
Subset Sum is an NP problem
81
SAT (Boolean satisfiability)
Given a Boolean function 𝑓𝑓(𝑥𝑥), 𝑥𝑥𝜖𝜖{0,1}𝑛𝑛 (with 𝑛𝑛 potentially
extremely large), 𝑓𝑓: {0,1}𝑛𝑛 → 0,1 .
Trivial application:
Electric circuit design. If the answer is “no”, replace circuitry
by ground.
82
SAT (Boolean Satisfiability)
SAT is an NP problem (Cook-Levin, 1971)
83
3-SAT (Boolean Satisfiability)
Both SAT and 3-SAT are NP-complete (Cook-Levin).
84
3-SAT (Boolean Satisfiability)
Both SAT and 3-SAT are NP-complete (Cook-Levin).
85
The Challenge
P = NP ?
One out of seven Millennium Prize Problems by the Clay Mathematics
Institute, each carrying a US $1,000,000 prize money.
86
History
S. Cook,
"The complexity of theorem proving procedures",
In Proc. 3rd Annual ACM Symp. On Theory of Computing, pp. 151—158,
1971
L. Levin,
"Универсальные задачи перебора"
("Universal enumeration problems"),
Problems of Information Tramsmission 9(3): 115—116, 1973.
87
Polynomial reduction
Input to P2 O(?) Answer P2
88
Polynomial reduction
Input to P2 O(?) Answer P2
PTIME, f1
Input to P1
89
Polynomial reduction
Input to P2 O(?) Answer P2
PTIME, f1
90
Polynomial reduction
Input to P2 O(?) Answer P2
PTIME, f1
PTIME, f3
91
Polynomial reduction
Input to P2 Answer P2
O(max(f1,f2,f3))
PTIME, f1
PTIME, f3
92
Reduce 3-SAT to Subset Sum
3-SAT is an NP problem. Is it NP-complete?
Reduce 3-SAT (in NP-complete) to subset sum.
Input:
n variables xi and m clauses (e.g. (¬x1 ∨ x2 ∨ x3) )
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
93
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "True" part
Construct numbers ti of n+m digits for each variable xi.
94
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "True" part
Construct numbers ti of n+m digits for each variable xi.
The ith digit of ti is 1.
95
Reduce 3-SAT to Subset Sum
Example:
(𝒙𝒙𝟏𝟏 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝒙𝒙𝟏𝟏 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "True" part
Construct numbers ti of n+m digits for each variable xi.
The ith digit of ti is 1.
Set values in second part to 1 if xi is in clause c.
96
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝒙𝒙𝟐𝟐 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝒙𝒙𝟐𝟐 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "True" part
Construct numbers ti of n+m digits for each variable xi.
The ith digit of ti is 1.
Set values in second part to 1 if xi is in clause c.
97
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝒙𝒙𝟑𝟑 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝒙𝒙𝟑𝟑 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝒙𝒙𝟑𝟑 )
Input Transform: "True" part
Construct numbers ti of n+m digits for each variable xi.
The ith digit of ti is 1.
Set values in second part to 1 if xi is in clause c.
98
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "True" part
Construct numbers ti of n+m digits for each variable xi.
The ith digit of ti is 1.
Set values in second part to 1 if xi is in clause c.
99
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "False" part
Construct numbers fi of n+m digits for each variable xi.
100
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "False" part
Construct numbers fi of n+m digits for each variable xi.
The ith digit of fi is 1.
101
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "False" part
Construct numbers fi of n+m digits for each variable xi.
Set values in second part to 1 if 𝑥𝑥�𝑖𝑖 is in clause c.
102
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "XY" part
Construct numbers xi=yi of n+m digits for each variable xi.
The "c=i" digit of xi,yi is 1.
103
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "S" part
Construct a number s of n+m digits for each variable xi.
The "i-part" digits of s are 1.
104
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "S" part
Construct a number s of n+m digits for each variable xi.
The "i-part" digits of s are 1.
The "c-part" digits of s are 3 (# of variables).
105
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: "S" part
Construct a numbers s of n+m digits for each variable xi.
The "i-part" digits of s are 1.
The "c-part" digits of s are 3 (# of variables).
106
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: Complexity
Construct 2n n+m numbers ti, fi: 𝑂𝑂(2𝑛𝑛2 + 6𝑛𝑛𝑛𝑛)
Construct 2m n+m numbers xi, yi: 𝑂𝑂 2𝑚𝑚𝑚𝑚 + 2𝑚𝑚2
Construct one n+m numbers s: 𝑂𝑂 𝑛𝑛 + 𝑚𝑚
Encoding 2(n+m)+1 numbers: O(2n2+4nm+2m2+n+m)
107
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: Complexity
Construct 2n n+m numbers ti, fi: 𝑶𝑶(𝟏𝟏𝟏𝟏𝟏𝟏𝟐𝟐)
Construct 2m n+m numbers xi, yi: 𝑂𝑂 2𝑚𝑚𝑚𝑚 + 2𝑚𝑚2
Construct one n+m numbers s: 𝑂𝑂 𝑛𝑛 + 𝑚𝑚
Encoding 2(n+m)+1 numbers: O(2n2+4nm+2m2+n+m)
108
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: Complexity
Construct 2n n+m numbers ti, fi: 𝑶𝑶(𝟏𝟏𝟏𝟏𝟏𝟏𝟐𝟐)
Construct 2m n+m numbers xi, yi: 𝑶𝑶 𝟖𝟖𝟖𝟖𝟐𝟐
Construct one n+m numbers s: 𝑂𝑂 𝑛𝑛 + 𝑚𝑚
Encoding 2(n+m)+1 numbers: O(2n2+4nm+2m2+n+m)
109
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: Complexity
Construct 2n n+m numbers ti, fi: 𝑶𝑶(𝟏𝟏𝟏𝟏𝟏𝟏𝟐𝟐)
Construct 2m n+m numbers xi, yi: 𝑶𝑶 𝟖𝟖𝟖𝟖𝟐𝟐
Construct one n+m numbers s: 𝑶𝑶 𝟒𝟒𝟒𝟒
Encoding 2(n+m)+1 numbers: O(2n2+4nm+2m2+n+m)
110
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: Complexity
Construct 2n n+m numbers ti, fi: 𝑶𝑶(𝟏𝟏𝟏𝟏𝟏𝟏𝟐𝟐)
Construct 2m n+m numbers xi, yi: 𝑶𝑶 𝟖𝟖𝟖𝟖𝟐𝟐
Construct one n+m numbers s: 𝑶𝑶 𝟒𝟒𝟒𝟒
Encoding 2(n+m)+1 numbers: O(18m2+12m2+2m2+…)
111
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: Complexity
Construct 2n n+m numbers ti, fi: 𝑶𝑶(𝟏𝟏𝟏𝟏𝟏𝟏𝟐𝟐)
Construct 2m n+m numbers xi, yi: 𝑶𝑶 𝟖𝟖𝟖𝟖𝟐𝟐
Construct one n+m numbers s: 𝑶𝑶 𝟒𝟒𝟒𝟒
Encoding 2(n+m)+1 numbers: O(32m2)
112
Reduce 3-SAT to Subset Sum
Example:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
Input Transform: Complexity
Construct 2n n+m numbers ti, fi: 𝑶𝑶(𝟏𝟏𝟏𝟏𝟏𝟏𝟐𝟐)
Construct 2m n+m numbers xi, yi: 𝑶𝑶 𝟖𝟖𝟖𝟖𝟐𝟐
Construct one n+m numbers s: 𝑶𝑶 𝟒𝟒𝟒𝟒
Encoding 2(n+m)+1 numbers: O(32m2)
113
Reduce 3-SAT to Subset Sum
Formula satisfiable if subset exists:
If xi true, pick ti, otherwise fi.
Pick xj if number of true literals in clause j is 1.
Also pick yj if number of true literals in clause j less than 3.
Always pick s (a negative number).
114
Reduce 3-SAT to Subset Sum
Formula satisfiable if subset exists:
If xi true, pick ti, otherwise fi.
Pick xj if number of true literals in clause j is 1.
Also pick yj if number of true literals in clause j less than 3.
Always pick s (a negative number).
We need a config
with 3x True for each
clause.
115
Reduce 3-SAT to Subset Sum
Formula satisfiable if subset exists:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
If xi true, pick ti, otherwise fi.
Pick xj if number of true literals in clause j is 1.
Also pick yj if number of true literals in clause j less than 3.
Always pick s (a negative number).
We need a config with 3x
True for each clause.
116
Reduce 3-SAT to Subset Sum
Formula satisfiable if subset exists:
(𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 ) ∧ (𝑥𝑥1 ∨ 𝑥𝑥2 ∨ 𝑥𝑥3 )
If xi true, pick ti, otherwise fi.
Pick xj if number of true literals in clause j is 1.
Also pick yj if number of true literals in clause j less than 3.
Always pick s (a negative number).
We need a config with 3x
True for each clause.
117
Reduce 3-SAT to Subset Sum
Subset Sum is an NP problem. Is it NP-complete?
Reduce 3-SAT to subset sum.
Done?
Almost…
Output Transform:
If exists subset L, sum(L)==0: True, else False.
Identity transform, O(1).
118
Polynomial reduction
Input to P2 Answer P2
O(max(m2,f p))
O(m2)
O(1)
Polynomial?
Input to P1 O(fp (m)) Output of P1
119
Reduce 3-SAT to SubSet Sum
Fundamental (!!!) Insight with Reduction:
IF Subset Sum can be solved in PTIME,
THEN 3-SAT can be solved in PTIME.
(We don't know whether Subset Sum is in P)
120
NP-complete
You solve one in PTIME, you solve all in PTIME, thanks to established
polynomial reductions.
121
P≠NP?
Under the assumption P≠NP:
P Problems are a subset of NP (can be quickly verified)
NP complete problems are a subset of NP.
(99% of all experts believe this to be true)
122
P≠NP?
Under the assumption P=NP:
P Problems are a subset of NP (can be quickly verified)
NP complete problems are a subset of NP
(not very likely)
123
Brainteaser
How bad is 𝑶𝑶(𝑵𝑵!) ?
𝑁𝑁
Stirling approximation: 𝑁𝑁! ~ 2𝜋𝜋𝜋𝜋 𝑁𝑁⁄𝑒𝑒
This is much worse than 𝑂𝑂 𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 𝑁𝑁 …
https://dx.doi.org/10.1109/TVCG.2020.3030451
124