1.8 KiB
Executable File
1.8 KiB
Executable File
Create an Adjacency Matrix for the following Weighted Undirected Graph.
A-3-B
| |
2 1
| |
C-4-D
To create an adjacency matrix for the given weighted undirected graph, we need to follow these steps:
- Identify the nodes: We have 4 nodes:
A
,B
,C
, andD
. - Create a matrix: The matrix will be 4x4, where rows and columns represent the nodes, and the elements in the matrix represent the weights of the edges between nodes. If there's no edge between two nodes, the matrix will have a value of
0
(or infinity, depending on the context, but we'll use0
here). - Place the edge weights in the matrix: Since the graph is undirected, the matrix will be symmetric, meaning if there's an edge from node
A
to nodeB
, the matrix entry at[A][B]
and[B][A]
will have the same value.
Graph Details:
- A -3B: There is an edge between A and B with weight 3.
- A -2C: There is an edge between A and C with weight 2.
- B -1D: There is an edge between B and D with weight 1.
- C -4D: There is an edge between C and D with weight 4.
Adjacency Matrix
Let's map the nodes to indices:
A
= 0B
= 1C
= 2D
= 3
Matrix:
A | B | C | D | |
---|---|---|---|---|
A | 0 | 3 | 2 | 0 |
B | 3 | 0 | 0 | 1 |
C | 2 | 0 | 0 | 4 |
D | 0 | 1 | 4 | 0 |
Explanation:
A-B
has a weight of 3, somatrix[0][1] = 3
andmatrix[1][0] = 3
(since the graph is undirected).A-C
has a weight of 2, somatrix[0][2] = 2
andmatrix[2][0] = 2
.B-D
has a weight of 1, somatrix[1][3] = 1
andmatrix[3][1] = 1
.C-D
has a weight of 4, somatrix[2][3] = 4
andmatrix[3][2] = 4
.
This adjacency matrix accurately represents the weighted undirected graph.