0% found this document useful (0 votes)
2 views

Tutorial 12.3 - Tutorial on pseudocode for graph, adjacency matrix and dictionary for graph or adjacency matrix

The document is a tutorial on pseudocode related to graphs, specifically focusing on adjacency matrices and dictionary representations. It includes examples of creating a matrix filled with zeros and provides a detailed pseudocode procedure for generating such matrices. Additionally, it presents a sample adjacency matrix and lists edges with their corresponding values.

Uploaded by

Meera Shridhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Tutorial 12.3 - Tutorial on pseudocode for graph, adjacency matrix and dictionary for graph or adjacency matrix

The document is a tutorial on pseudocode related to graphs, specifically focusing on adjacency matrices and dictionary representations. It includes examples of creating a matrix filled with zeros and provides a detailed pseudocode procedure for generating such matrices. Additionally, it presents a sample adjacency matrix and lists edges with their corresponding values.

Uploaded by

Meera Shridhar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Prof. Madhavan Mukund Prof. G.

Venkatesh
Department of Computer Science Indian Institute of Technology Madras
Chennai Mathematical Institute

Mr. Omkar Joshi


Course Instructor
IITM Online Degree Programme
Content
▪ Graph
▪ Adjacency matrix
▪ Dictionary representation of a graph/adjacency matrix
▪ Pseudocode to create matrix with all zeros using dictionary

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 1
TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 2
0 1 2 3 4
0 0 3 0 0 8
1 0 0 1 4 0
2 0 0 0 0 0
3 7 0 2 0 0
4 0 0 0 3 0

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 3
0 1 2 3 4
0 0 3 0 0 8
1 0 0 1 4 0
2 0 0 0 0 0
3 7 0 2 0 0
4 0 0 0 3 0

matrix = {0: {0: 0, 1: 3, 2: 0, 3: 0, 4: 8},


1: {0: 0, 1: 0, 2: 1, 3: 4, 4: 0},
2: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
3: {0: 7, 1: 0, 2: 2, 3: 0, 4: 0},
4: {0: 0, 1: 0, 2: 0, 3: 3, 4: 0}}

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 4
0 1 2 3 4
0 0 3 0 0 8
1 0 0 1 4 0
2 0 0 0 0 0
3 7 0 2 0 0
4 0 0 0 3 0
List of edges
matrix[0][1] == 3
matrix = {0: {0: 0, 1: 3, 2: 0, 3: 0, 4: 8}, matrix[0][4] == 8
1: {0: 0, 1: 0, 2: 1, 3: 4, 4: 0}, matrix[1][2] == 1
2: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}, matrix[1][3] == 4
3: {0: 7, 1: 0, 2: 2, 3: 0, 4: 0}, matrix[3][0] == 7
4: {0: 0, 1: 0, 2: 0, 3: 3, 4: 0}} matrix[3][2] == 2
matrix[4][3] == 3

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 5
Procedure createMatrix (rows, cols)
matrix = {}
i=0
while (i < rows) {
matrix[i] = {}
j=0
while (j < cols) {
matrix[i][j] = 0
j=j+1
}
i=i+1
}
return (matrix)
end createMatrix

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 6
Procedure createMatrix (rows, cols) e.g. matrix = createMatrix (5, 5)
matrix = {}
i=0
matrix = {}
while (i < rows) {
matrix[i] = {}
j=0
while (j < cols) {
matrix[i][j] = 0
j=j+1
}
i=i+1
}
return (matrix)
end createMatrix

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 7
Procedure createMatrix (rows, cols) i j e.g. matrix = createMatrix (5, 5)
0
matrix = {}
i=0
matrix = {0: {}}
while (i < rows) {
matrix[i] = {}
j=0
while (j < cols) {
matrix[i][j] = 0
j=j+1
}
i=i+1
}
return (matrix)
end createMatrix

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 8
Procedure createMatrix (rows, cols) i j e.g. matrix = createMatrix (5, 5)
0 0
matrix = {}
i=0
matrix = {0: {0: 0}}
while (i < rows) {
matrix[i] = {}
j=0
while (j < cols) {
matrix[i][j] = 0
j=j+1
}
i=i+1
}
return (matrix)
end createMatrix

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 9
Procedure createMatrix (rows, cols) i j e.g. matrix = createMatrix (5, 5)
0 0
matrix = {} 1
2
i=0 3
4 matrix = {0: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}}
while (i < rows) {
matrix[i] = {}
j=0
while (j < cols) {
matrix[i][j] = 0
j=j+1
}
i=i+1
}
return (matrix)
end createMatrix

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 10
Procedure createMatrix (rows, cols) i j e.g. matrix = createMatrix (5, 5)
0 0
matrix = {} 1
2
i=0 3
4 matrix = {0: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
while (i < rows) {
1 0 1: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}}
matrix[i] = {} 1
2
j=0 3
while (j < cols) { 4

matrix[i][j] = 0
j=j+1
}
i=i+1
}
return (matrix)
end createMatrix

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 11
Procedure createMatrix (rows, cols) i j e.g. matrix = createMatrix (5, 5)
0 0
matrix = {} 1
2
i=0 3
4 matrix = {0: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
while (i < rows) {
1 0 1: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
matrix[i] = {} 1
2 2: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}}
j=0 3
while (j < cols) { 4
2 0
matrix[i][j] = 0 1
2
j=j+1 3
} 4

i=i+1
}
return (matrix)
end createMatrix

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 12
Procedure createMatrix (rows, cols) i j e.g. matrix = createMatrix (5, 5)
0 0
matrix = {} 1
2
i=0 3
4 matrix = {0: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
while (i < rows) {
1 0 1: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
matrix[i] = {} 1
2 2: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
j=0 3 3: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}}
while (j < cols) { 4
2 0
matrix[i][j] = 0 1
2
j=j+1 3
} 4
3 0
i=i+1 1
2
} 3
return (matrix) 4

end createMatrix

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 13
Procedure createMatrix (rows, cols) i j e.g. matrix = createMatrix (5, 5)
0 0
matrix = {} 1
2
i=0 3
4 matrix = {0: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
while (i < rows) {
1 0 1: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
matrix[i] = {} 1
2 2: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
j=0 3 3: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0},
while (j < cols) { 4
2 0 4: {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}}
matrix[i][j] = 0 1
2
j=j+1 3
} 4
3 0
i=i+1 1
2
} 3
return (matrix) 4
4 0
end createMatrix 1
2
3
4

TUTORIAL ON PSEUDOCODE FOR GRAPH, ADJACENCY MATRIX AND DICTIONARY FOR GRAPH/ADJACENCY MATRIX 14

You might also like