CH 02
CH 02
On
Computer
Discrete Math 2
Content
• Represent the graph with the adjacency matrix
– Properties of path on adjacency matrices
– Order Matrix and Induction Matrix
• Representing the graph with the association matrix
• Representing a graph using an edge list
• Representing a graph using an adjacency list
2
Represent the graph with the adjacency
matrix
The adjacency matrix of an undirected graph
• Consider a single undirected graph G =<V, E>, with a set of
vertices V = {1, 2, . . ., n}, edge set E = {e 1 , e 2 ,.., e m }. We call
the adjacency matrix of the graph G a matrix whose elements
are either 0 or 1 according to the following rules:
4
Adjacent matrix properties for undirected graphs
5
The adjacency matrix of a directed graph
• The definition is very similar to that of an undirected
graph
– Attention should be paid to the direction of the edge
– The adjacency matrix of a directed graph is asymmetric
6
Properties of adjacency matrices of directed graphs
7
Properties of the path on the graph using the
adjacency matrix
8
Properties of paths on graphs using adjacency
matrices (example)
9
Weight Matrix
10
Advantages & disadvantages of adjacency
matrices
• Advantage
– Simple, easy to install on the computer
– Use a two-dimensional array to represent the adjacency matrix
– The earrings still show
– It is easy to check two vertices u ,v are adjacent to each other?
• Exactly one comparison ( a[u][v]≠0?)
• Disadvantage
– Waste of memory: no matter how many or few edges we need n2
memory units for representation
– Cannot be represented with graphs with a large number of vertices
– To see what adjacent vertices u has, it takes n comparisons, even if u is
an isolated vertex or a suspended vertex.
– Can't represent parallel edges
11
Degree Matrix
• -order matrix of the graph G is an nxn square
matrix defined as follows:
12
Induction Matrix
• Induction matrix (Laplace matrix or Kirchhoff matrix)
• Given a graph G with n vertices, the induction matrix is
defined as follows: L=DA
14
Code to represent graph using adjacency matrix
15
Representing the graph with the association
matrix
Membership Matrix: Undirected Graph
17
Membership Matrix: Directed Graph
18
What are the advantages and disadvantages of
association matrices?
• Advantage
– Looking at the matrix we know the number of edges, the
number of vertices, degrees and semi-degrees of that
vertex.
– Know the direction of an edge from which vertex goes to
which vertex (directed graph).
• Disadvantage
– Complicated representation if the graph has a
large number of edges => difficult to represent.
19
Representing a graph using an edge list
Undirected graph using an edge list
• Only needs to list the edges (u,v) but not the edges (v,u).
• Edges should be listed in ascending order of the first vertex of each
edge.
• The edge list property of an undirected graph:
– The first vertex is smaller than the last vertex of each edge.
– The number of edges whose value u belongs to both the first and last
vertices of the edge list is the degree of vertex u.
21
Directed graph representation using edge lists
• Each edge is a tuple taking into account the order of the vertices.
• Pay special attention to the direction of the edges
• The edge list property of a directed graph :
– The leading vertex is not necessarily smaller than the end vertex of each edge.
– The number of edges with value u in the top column of the edges is deg+ (u).
– The number of edges whose u value belongs to the vertex column at the end of the
edges is deg- (u).
22
Weight graph using edge list
23
Edge list (arcs)
• In the case of graphs with few edges (graphs with the number of
edges m 6n), people often represent the graph as an edge list.
– Stores a list of all edges (arcs) of an undirected (directed) graph.
– Each edge (arc) e(x, y) is corresponding to two variables dau[e], cuoi[e].
– To store the graph, we need 2m memory units.
– If it is a weighted graph, we need m more memory units to store the weights
of the edges (2m+m= 3m memory units in total).
24
Advantages & disadvantages of edge lists
• Advantages of edge lists:
– In the case of a graph with few edges (m 6n ), representing an edge
list saves memory space
– It is advantageous for some algorithms to only care about the edges
of the graph.
• Disadvantages of edge lists:
– When it is necessary to traverse the vertices adjacent to the vertex u,
it is imperative to traverse all the edges of the graph.
• This makes the algorithm very computationally expensive.
25
Edge List Storage Format
• The first line records the number n, m corresponding to the number of
vertices and the number of edges of the graph.
– Two numbers are written side by side with a few spaces;
• m lines, each of which represents an edge of the graph
– trailing vertices of each edge are written a few spaces apart.
26
Data structure representing an edge list (1/4)
27
Data structure representing edge list (2/4)
• For example:
28
Data structure representing an edge list (3/4)
29
Data structure representing edge list (4/4)
30
Code to represent graph using edge list
31
Representing a graph using an adjacency list
Adjacent list
• Each vertex u of the graph we store a list of adjacent vertices which
we denote by Ke(u), that is
33
Advantages & disadvantages of adjacency lists
• Advantages of adjacency list:
– Easily traverse all vertices of an adjacency list;
– Easily traverse the edges of the graph in each adjacency list;
– Optimal representation and storage methods. Especially adjacency lists in arrays.
• Disadvantages of adjacency list:
– Difficult for readers with programming skills weak.
– The implementation of the problem using an adjacency list is relatively longer
than that of an adjacency matrix and an edge list.
34
Representing an adjacency list using an Array
• array is divided into n segments
– second paragraph i in the array stores the adjacency list of
the i V
– To know from which element an array segment starts to
which element, we use another array to store the end
positions of each segment .
• Head[ i ] is the end index of the segment managing the adjacent vertex of i-1.
– Or Head[i]+1 is the starting index of the segment that manages the adjacent vertex of i.
The index of the segment containing the adjacent vertices of i is from Head[i]+1 to Head[i+1]
35
Representing an Adjacent List Using a Linked List
36
Adjacent list storage format (using Arrays)
• The first line records the number of vertices of the graph
– WOMEN The next line records the adjacency list of the corresponding vertex in the
format:
• The first element is the end position of the segment , followed by the list of
vertices of the adjacency list
– Elements are written a few spaces apart
37
Graph representation using an Supply List (using Arrays)
38
Graph representation using Edge Lists (using Arrays)
• Step-by-step description:
– Initially Head[i]=0
– Read each edge (u,v) and increment
Head[u]=Head[u]+1.
– Head[i]=Head[i-1]+Head[i] stack. i=2
→ n+1;
• Note: Head[n+1] is always m if it is a
directed graph.
– Traverse the edges, Adj[Head[u]] = v;
Head[u]=Head[u]-1;
40
Exercise 1
a) A single undirected graph with n How many edges does a vertex have
at most?
b) During a meeting, some guests shake hands with other guests. Show
that the total number of handshakes of all guests is even.
41
Exercise 2
Present the undirected graph by:
1) Adjacent Matrix
2) Edge list
3) Adjacent list
❖ Write a program that reads data from the file in the form of
an adjacency matrix and prints to the screen a list of the
edges of the graph, and the degree of each vertex.
42
Exercise 3
Present the directed graph by:
1) Adjacent Matrix
2) Edge list
3) Adjacent list
❖ Write a program that reads data from the file in the form of
an adjacency list , prints the adjacency matrix and the semi-
in/out degree of each vertex.
43
Exercise 4
Present the weight graph by:
1) Adjacent Matrix
2) Edge list
3) Adjacent list
❖ Write a program that reads data from the file in the form of a
list of sectors, prints the adjacency matrix and the sum of the
even/odd semi degrees. 44
Exercise 5
• The chessboard 8 x 8 is numbered as follows:
• Each cell can be considered as a vertex of the graph. Two vertices are considered adjacent if a
king placed in one square can jump to the other square after one move. For example: cell 1 is
adjacent to cell 2, 9, 10, cell 11 is adjacent to 2, 3, 4, 10, 12, 18, 19, 20.
• Write a program to generate the adjacency matrix of the graph, print the results to the file
king.out.
45
Exercise 6
• The chessboard 8 8 is numbered as follows:
• Each cell can be considered as a vertex of the graph. Two vertices are said to be adjacent if a horse
placed in one square can jump to the other square after a move. For example, cell 1 is adjacent to
11, 18, and cell 11 is adjacent to 1, 5, 17, 21, 26, 28.
• Write a program to generate the adjacency matrix of the graph, the results are written to the file
horse.out.
46