Class01 Computer Contest Level 3 Notes
Class01 Computer Contest Level 3 Notes
Bruce Nan
Email: xiaomingnan@gmail.com
Seven Bridges of Königsberg
The Seven Bridges of Königsberg is a notable historical
problem in mathematics. The problem was to find a walk
through the city that would cross each bridge once and only
once. The islands could not be reached by any route other
than the bridges, and every bridge must have been crossed
completely every time.
Euler’s Analysis
During any walk in the graph, the
number of times one enters a non-
C terminal vertex equals the number of
times one leaves it.
If every bridge is traversed exactly
once it follows that for each land mass
(except possibly for the ones chosen
for the start and finish), the number of
A B bridges touching that land mass is
even.
However, all the four land masses in
the original problem are touched by an
odd number of bridges (one is touched
by 5 bridges and the other three by 3).
D Since at most two land masses can
serve as the endpoints of a putative
walk, the existence of a walk traversing
each bridge once leads to a
contradiction.
Eulerian path
E= {(a,b),(a,c),(a,d),
c (b,e),(c,d),(c,e),
(d,e)}
d e
Applications CS16
electronic circuits
JFK
LAX STL
HNL
DFW
FTL
Directed Vs. Undirected Graph
n 1
e ( di ) / 2
Why? Since adjacent vertices each
count the adjoining edge, it will be
0
counted twice
Examples 0
3 2
0 1 2
3 3
3 1 2 3 3 4 5 6
3 1 1 G2 1 1
G1 3
0 in:1, out: 1
directed graph
in-degree
out-degree 1 in: 1, out: 2
2 in: 1, out: 0
G3
Terminology: Path
3 2
path: sequence of
vertices v1,v2,. . .vk such
that consecutive vertices 3
vi and vi+1 are adjacent.
The length of such a path 3 3
is the number of edges
on the path, which is a b a b
equal to k-1. We allow a
path from a vertex to c c
itself; if this path contains
no edges, then the path d e d e
lenght is 0. abedc bedc
More Terminology
simple path: no repeated vertices
a b
bec
c
d e
cycle: simple path, except that the last vertex is the
same as the first vertex
a b
acda
c
d e
More Terminology
An undirected graph is connected if there is a
path from every vertex to every other vertex.
A directed graph with this property is called
strongly connected.
More Terminology
1 1 1 1
2 2 2
(i) (ii) (iii) (iv)
G3 (b) Some of the subgraph of G3
Tree and Forests
tree
forest
tree
tree
Examples
V1 V2 V1 V2
Tree
V3 V3
V4 V5 V4 V5
V1 V2 V1 V2 V3 V6
V3 V6 Forest
V7
V7
V4 V5
V4 V5
Connectivity
Let n = #vertices, and m = #edges
A complete graph: one in which all pairs of vertices are
adjacent
How many total edges in a complete graph?
Each of the n vertices is incident to n-1 edges,
n 5
m (5
More Connectivity
n = #vertices n5
m = #edges m4
For a tree m = n - 1
If m < n - 1, G
is not connected
n5
m3
How to store a Graph?
Adjacent Matrix
Adjacent Lists
Adjacent Matrix
G4
More Adjacent Matrix
vertex= V1 V2 V3 V4
V1 V4 V1 V2 V3 V4
0 1 0 1 V1
arc=
1 0 1 1 V2
V2 V3 0 1 0 0 V3
1 1 0 0 V4
arc=
1 0 1 1 V2
V2 V3 0 1 0 0 V3
1 1 0 0 V4
vertex= V1 V2 V3 V4
V1 V4 V1 V2 V3 V4
0 1 0 1 V1
arc=
1 0 1 1 V2
V2 V3 0 1 0 0 V3
1 1 0 0 V4
How to find all adjacent vertices of vi?
Traverse all ith row elements, if arc[i][j] is 1, vj is
the adjacent vetex of vi
Digraph Adjacent Matrix
vertex= V1 V2 V3 V4
V1 V2 V1 V2 V3 V4
0 1 1 0 V1
arc=
0 0 0 0 V2
V3 V4 0 0 0 1 V3
1 0 0 0 V4
vertex= V1 V2 V3 V4
V1 V2 V1 V2 V3 V4
0 1 1 0 V1
arc=
0 0 0 0 V2
V3 V4 0 0 0 1 V3
1 0 0 0 V4
n 1 n 1
ind (vi ) A[ j , i ] outd (vi ) A[i , j ]
j 0 j 0
Create Adjacent Matrix
buildGraph(int a[ ], int n, int e)
{
vertexNum=n; arcNum=e;
for (i=0; i<vertexNum; i++)
vertex[i]=a[i];
for (i=0; i<vertexNum; i++) //initialization
for (j=0; j<vertexNum; j++)
arc[i][j]=0;
for (k=0; k<arcNum; k++) //get each edge
{
cin>>i>>j; //the vertices of the edge
arc[i][j]=1; arc[j][i]=1; //adjacency
}
}
Practice Time
Implementation
Vector array
0 0 4
2 1 5
1 2 3 6
3 7
0 1 2 3 0 1 2
1 0 2 3 1 0 3
2 0 1 3 2 0 3
3 0 1 2 3 1 2
G1 0 4 5
5 4 6
0 1 6 5 7
1 0 2 1
7 6
2
G3 G4
2
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
More Adjacency List
degree of a vertex in an undirected graph
–# of nodes in adjacency list
# of edges in a graph
–determined in O(n+e)
out-degree of a vertex in a directed graph
–# of nodes in its adjacency list
in-degree of a vertex in a directed graph
–traverse the whole data structure
Graph Traversal
A B C D A B C D
E F G H E F G H
I J K L I J K L
M N O P M N O P
0 1 2
0 1 2 3
A B C D A B C D
E F G H E F G H
I J K L
I J K L
M N O P
M N O P
V1
V1
V2 V3
V4 V5 V6 V7
V8
Result: V1
V1
V2 V3
V2 V3
V4 V5 V6 V7
V8
Result: V1 V2 V3
V1
V3 V4 V5
V2 V3
V4 V5 V6 V7
V8
Result: V1 V2 V3 V4 V5
V1
V4 V5 V6 V7
V2 V3
V4 V5 V6 V7
V8
Result: V1 V2 V3 V4 V5 V6 V7
V1
V5 V6 V7 V8
V2 V3
V4 V5 V6 V7
V8
Result: V1 V2 V3 V4 V5 V6 V7 V8
Code for BFS
void BFS( TABLE T )
{
QUEUE Q;
vertex v, w;
Q = create_queue( NUM_VERTEX ); make_null( Q );
enqueue( s, Q );
while( !is empty( Q ) )
{
v = dequeue( Q );
T[v].known = TRUE; /* not really needed anymore */
for each w adjacent to v
if( T[w].dist = INT_MAX )
{
T[w].dist = T[v].dist + 1;
T[w].path = v;
enqueue( w, Q );
}
}
dispose_queue( Q );
Code – BFS Using Adjacent Matrix
template <class T>
void MGraph::BFSTraverse(int v)
{
front=rear=-1; //Assuming no overflow
cout<<vertex[v]; visited[v]=1; Q[++rear]=v;
while (front!=rear)
{
v=Q[++front];
for (j=0; j<vertexNum; j++)
if (arc[v][j]==1 && visited[j]==0 ) {
cout<<vertex[j]; visited[j]=1; Q[++rear]=j;
}
}
}
Code – BFS Using Adjacent List
template <class T>
void ALGraph::BFSTraverse(int v)
{
front=rear=-1;
cout<<adjlist[v].vertex; visited[v]=1; Q[++rear]=v;
while (front!=rear)
{
v=Q[++front]; p=adjlist[v].firstedge;
while (p!=NULL)
{
j= p->adjvex;
if (visited[j]==0) {
cout<<adjlist[j].vertex; visited[j]=1; Q[++rear]=j;
}
p=p->next;
}
}
}
BFS Usage
Finding a path
In a maze, how to find a path from the start to the
end using least steps?
Exploiting traversal