You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicclassAdjacencyMatrix{privateint[,]matrix;privateintvertices;publicAdjacencyMatrix(intv){vertices=v;matrix=newint[v,v];}// Thêm cạnh vô hướngpublicvoidAddEdge(intsource,intdest){matrix[source,dest]=1;matrix[dest,source]=1;}// Thêm cạnh có hướngpublicvoidAddDirectedEdge(intsource,intdest){matrix[source,dest]=1;}// Kiểm tra cạnh tồn tạipublicboolHasEdge(intsource,intdest){returnmatrix[source,dest]==1;}// In ma trận kềpublicvoidPrintMatrix(){for(inti=0;i<vertices;i++){for(intj=0;j<vertices;j++){Console.Write(matrix[i,j]+" ");}Console.WriteLine();}}}
2.1.2. Ma trận trọng số
publicclassWeightMatrix{privateint[,]matrix;privateintvertices;privateconstintINF=int.MaxValue;publicWeightMatrix(intv){vertices=v;matrix=newint[v,v];// Khởi tạo với giá trị vô cùngfor(inti=0;i<v;i++)for(intj=0;j<v;j++)matrix[i,j]=INF;}// Thêm cạnh có trọng sốpublicvoidAddEdge(intsource,intdest,intweight){matrix[source,dest]=weight;matrix[dest,source]=weight;}// Thêm cạnh có hướng và trọng sốpublicvoidAddDirectedEdge(intsource,intdest,intweight){matrix[source,dest]=weight;}// Lấy trọng số cạnhpublicintGetWeight(intsource,intdest){returnmatrix[source,dest];}// In ma trận trọng sốpublicvoidPrintMatrix(){for(inti=0;i<vertices;i++){for(intj=0;j<vertices;j++){if(matrix[i,j]==INF)Console.Write("∞ ");elseConsole.Write(matrix[i,j]+" ");}Console.WriteLine();}}}
2.1.3. Ưu nhược điểm
Ưu điểm:
Dễ cài đặt và sử dụng
Kiểm tra cạnh tồn tại nhanh O(1)
Phù hợp với đồ thị dày (nhiều cạnh)
Nhược điểm:
Tốn bộ nhớ O(V²)
Không hiệu quả với đồ thị thưa
Thêm/xóa đỉnh tốn thời gian
2.2. Biểu diễn đồ thị bằng danh sách kề
2.2.1. Cài đặt cơ bản
publicclassAdjacencyList{privateList<int>[]adjList;privateintvertices;publicAdjacencyList(intv){vertices=v;adjList=newList<int>[v];for(inti=0;i<v;i++)adjList[i]=newList<int>();}// Thêm cạnh vô hướngpublicvoidAddEdge(intsource,intdest){adjList[source].Add(dest);adjList[dest].Add(source);}// Thêm cạnh có hướngpublicvoidAddDirectedEdge(intsource,intdest){adjList[source].Add(dest);}// Kiểm tra cạnh tồn tạipublicboolHasEdge(intsource,intdest){returnadjList[source].Contains(dest);}// In danh sách kềpublicvoidPrintGraph(){for(inti=0;i<vertices;i++){Console.Write($"Đỉnh {i}: ");foreach(intvinadjList[i]){Console.Write($"{v} ");}Console.WriteLine();}}}
2.2.2. Danh sách kề có trọng số
publicclassWeightedAdjacencyList{privateclassEdge{publicintDest;publicintWeight;publicEdge(intd,intw){Dest=d;Weight=w;}}privateList<Edge>[]adjList;privateintvertices;publicWeightedAdjacencyList(intv){vertices=v;adjList=newList<Edge>[v];for(inti=0;i<v;i++)adjList[i]=newList<Edge>();}// Thêm cạnh có trọng sốpublicvoidAddEdge(intsource,intdest,intweight){adjList[source].Add(newEdge(dest,weight));adjList[dest].Add(newEdge(source,weight));}// Thêm cạnh có hướng và trọng sốpublicvoidAddDirectedEdge(intsource,intdest,intweight){adjList[source].Add(newEdge(dest,weight));}// Lấy trọng số cạnhpublicintGetWeight(intsource,intdest){foreach(EdgeeinadjList[source]){if(e.Dest==dest)returne.Weight;}return-1;// Không tồn tại cạnh}// In danh sách kề có trọng sốpublicvoidPrintGraph(){for(inti=0;i<vertices;i++){Console.Write($"Đỉnh {i}: ");foreach(EdgeeinadjList[i]){Console.Write($"({e.Dest},{e.Weight}) ");}Console.WriteLine();}}}
2.2.3. Ưu nhược điểm
Ưu điểm:
Tiết kiệm bộ nhớ O(V + E)
Phù hợp với đồ thị thưa
Dễ dàng thêm/xóa cạnh
Nhược điểm:
Kiểm tra cạnh tồn tại chậm O(deg(v))
Cài đặt phức tạp hơn ma trận
Khó thực hiện một số thuật toán
2.3. Biểu diễn đồ thị bằng danh sách cạnh
2.3.1. Cài đặt cơ bản
publicclassEdgeList{privateclassEdge{publicintSource;publicintDest;publicintWeight;publicEdge(ints,intd,intw=1){Source=s;Dest=d;Weight=w;}}privateList<Edge>edges;privateintvertices;publicEdgeList(intv){vertices=v;edges=newList<Edge>();}// Thêm cạnhpublicvoidAddEdge(intsource,intdest,intweight=1){edges.Add(newEdge(source,dest,weight));}// Kiểm tra cạnh tồn tạipublicboolHasEdge(intsource,intdest){returnedges.Any(e =>e.Source==source&&e.Dest==dest);}// In danh sách cạnhpublicvoidPrintEdges(){foreach(Edgeeinedges){Console.WriteLine($"{e.Source} -> {e.Dest} (w={e.Weight})");}}}