Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

BÁO CÁO CHUYÊN ĐỀ THỰC TẬP NGÀNH

Đề số 05:


Tìm hiểu các loại đồ thị: có hướng, vô hướng, đồ thị có trọng số và
viết chương trình “biểu diễn đồ thị bằng ma trận kề - trọng số”

Giảng viên hướng dẫn: Ths. Nguyễn Thành Huy

Sinh viên thực hiện: Đàm Thu Hằng - 1910A02


Đinh Thị Lam
- 1910A02
Trần Thị Hồng
- 1910A02
1. Thiết kế modules
2. Mã giả

03/08/2024 2 / 22
I. Thiết kế modules

03/08/2024 3 / 22
03/08/2024 4/55
II. Mã giả

03/08/2024 5 / 22
1.Thêm đỉnh
Class GuiNodeManager Class Graph
PROCEDURE DrawNode(tdx, tdy, nameVertex) PROCEDURE AddVertex(nameVertex, tdx, tdy)
ellipse = Tạo ellipse và đặt các thuộc weight = increase size
tính Thêm nameVertext vào
height, weight, color vertIndexToName
textBlock = Tạo TextBlock và giá trị của vertNameToIndex[nameVertext] = n;
textBlock là nameVertex Thêm cặp (tdx, tdy) vào vertexPoints
grid = Tạo Grid và sự kiện đi kèm verticesNumber += 1;
MouseLeftButtonDown ENDPROCEDURE
Thêm ellipse vào grid
Thêm textBlock vào grid Class GuiNodeManager
Thêm grid vào GUI PROCEDURE AddNode(tdx, tdy, nameVertex)
gridsByVertexName[nameVertex] = grid; CALL DrawNode(tdx, tdy, nameVertex)
ENDPROCEDURE CALL graph.AddVertex(nameVertex,
tdx, tdy)
ENDPROCEDURE
03/08/2024 6 / 22
2.Xóa đỉnh
Class GuiNodeManage Class Graph
PROCEDURE RemoveNode(node) PROCEDURE RemoveVertext (vertextName)
edges =graph.GetEdgesByVertexName(vertexName) IF !IsVertext
Foreach edge in edges Vertext không tồn tại
guiLineManager.RemoveEdge(edge) THEN
CALL graph.RemoveVertex(vertexName) weight = reduse size
Xóa node khỏi GUI Xóa cặp (tdx, tdy) ở vertexPoints
vertextNumber -=1
Xóa nameVertex khỏi gridsByVertextName
ENDIF
ENDPROCEDURE ENDPROCEDURE
PROCEDURE Node_MouseLeftButtonDown(sender, e)
CALL RemoveNode(node)
ENDPROCEDURE

03/08/2024 7 / 22
2.Xóa đỉnh
Class Graph
FUNCTION GetEdgesByVertexName(vertexName) RETURNS List
edges = EmptyList
vertexIndex = Lấy index dựa vào vertexName
FOR i = 0 to verticesNumber - 1
IF weight[i, vertexIndex] > 0
THEN
Thêm cặp (vertIndexToName[i], vertIndexToName[vertexIndex]) vào edges
ENDIF
IF weight[vertexIndex, i] > 0
THEN
Thêm cặp (vertIndexToName[vertexIndex], vertIndexToName[i]) vào edges
ENDIF
ENDFOR
RETURN edges
ENDFUNCTION

03/08/2024 8 / 22
3. Thêm Cạnh
Class GuiLineManager Class GuiLineManager
PROCEDURE DrawArrowLine(nodeStart,nodeEnd, PROCEDURE AddArrowLine(nodeStart,nodeEnd,
weight, tdx1,tdy1,tdx2,tdy2) weight, tdx1,tdy1,tdx2,tdy2)
ArrowLine arrowLine = new ArrowLine(); CALL graph.AddEdge(weight, index of nodeStart,
Đặt tọa độ của arrowLine phụ thuộc vào tdx1, index of nodeEnd)
tdy1, tdx2, tdy2 CALL DrawArrowLine(nodeStart,nodeEnd, weight,
Đặt các thuộc tính của mũi tên như độ dài, màu sắc tdx1,tdy1,tdx2,tdy2)
Thêm sự kiện MouseLeftButtonDown vào arrowLine ENDPROCEDURE
Thêm mũi tên vào GUI
ENDPROCEDURE Class Graph
PROCEDURE AddEdge(weight, vtBD, vtKT)
weight[vtBD, vtKT] = weight
ENDPROCEDURE

03/08/2024 9 / 22
3. Thêm Cạnh
Class GuiNodeManager
gridPast = null
PROCEDURE Node_MouseLeftButtonDown(sender,e)
gridPresent = Get Grid from sender
IF gridPast is null
THEN
gridPast = gridPresent
RETURN
ENDIF
nameNodeStart = Get name from gridPast
nameNodeEnd = Get name from gridPresent
weight = Lấy trọng số từ MainWindow
Lấy tdx1, tdy1 từ Canvas
Lấy tdx2, tdy2 từ Canvas
CALL guiLineManager.AddArrowLine(nameNodeStart, nameNodeEnd, weight, tdx1, tdy1, tdx2, tdy2)
ENDPROCEDURE

03/08/2024 10 / 22
4. Xóa Cạnh
Class GuiLineManager Class Graph
PROCEDURE RemoveEdge( startNode,
PROCEDURE RemoveArowLine() endNode, weight)
Dùng vòng lặp FOR duyệt qua các phần tử
startNode = Canvas.GetLeft(edge), Canvas.GetTop(edge)
đỉnh
endNode = startNode.X + edge.X2, startNode.Y +edge.Y2 IF vertices[i] == startNode
trả lại chỉ số của startNode
vertex1 -> nhận chỉ số vertex tại startNode IF vertices[i] == endNode
trả lại chỉ số của endNode
vertex2 -> nhận chỉ số vertex tại endNode Kiểm tra nếu đã có thì thêm cạnh
ELSE
graph.RemoveEdge(vertex1,vertex2)
Thông báo đỉnh nguồn hoặc
Remove arrowLine from canvas đích không hợp lệ
END PROCEDURE
Call DrawGraph()

03/08/2024 11 / 22
4. Xóa Cạnh
PROCEDURE
ArrowLine_MouseLeftButtonDown(sender,e)
arrowLine = Get ArrowLine
from sender
CALL
RemoveEdge(arrowLine)
ENDPROCEDURE

03/08/2024 12 / 22
4. Xóa Cạnh
Class GuiLineManager
FUNCTION GetArrowLine(nameNodestart, nameNodeEnd)
Duyệt qua các phần tử trên grapCanvas
FOR EACH child IN graphCanvas.Children
IF child is ArrowLine
Ép kiểu child thành ArrowLine để có thể truy cập các thuộc tính (arrowLine =
(ArrowLine)child)
Lấy tên đỉnh bắt đầu và đỉnh kết thúc của ArrowLine
IF startNode == startNodeName AND endNode ==
endNodeName is true
RETURN arrowLine
END IF
END IF
END FOR
RETURN null
END FUNCTION

03/08/2024 13 / 22
5. BFS
Class MainWindow Class GuiTraversal
PROCEDURE BFS_Click(sender, e) PROCEDURE StartBFS(vertexNameStart)
vertexNameStart = Get the starting vertex FOR i = 0 to graph.verticesNumber – 1
name input from dialog box visited[i] = false
CALL guiTraversal.StartBFS(vertexNameStart) ENDFOR
Q = EmptyQueue
ENDPROCEDURE
indexStart = graph.vertNameToIndex[vertexNameStart]
CALL BFS(indexStart, Q, visited)
ENDPROCEDURE

03/08/2024 14
5. BFS
Class GuiTraversal IF visited[v] is False
PROCEDURE BFS(indexStart, Q, visited) THEN
Enqueue(Q, indexStart) visited[v] = true
visited[indexStart] = true Enqueue(Q, v)
Change color of arrowLine to
WHILE not Empty(Q) DO LightPink
u = Dequeue(Q) Animate the color of ellipse at
Animate the color of ellipse at position u to position
Violet v to Black
FOR v = 0 to graph.verticesNumber – 1 ELSE
IF graph.weight[u][v] > 0 Change color of arrowLine to
THEN DarkGray
arrowLine = Get ArrowLine ENDIF
between ENDIF
vertices at position u and v ENDFOR
line = Line drew based on
Animate the color of ellipse at position u to Blue
coordinates of
ENDWHILE
arrowLine
Animate the line ENDPROCEDURE
03/08/2024
Remove line from GUI 15
6. DFS
Class MainWindow Class GuiTraversal
PROCEDURE DFS_Click(sender, e) PROCEDURE StartDFS(vertexNameStart)
vertexNameStart = Get the starting vertex FOR i = 0 to graph.verticesNumber – 1
name input from dialog box visited[i] = false
CALL guiTraversal.StartDFS(vertexNameStart) ENDFOR
S = EmptyStack
ENDPROCEDURE
indexStart = graph.vertNameToIndex[vertexNameStart]
CALL DFS(indexStart, S, visited)
ENDPROCEDURE

03/08/2024 16
7. DFS
Class GuiTraversal
PROCEDURE DFS(indexStart, S, visited)
linesVisited = EmptySet
Push(S, indexStart)
WHILE not Empty(S) DO
u = Pop(S)
Animate the color of ellipse at position u to Violet
v=0
FOR v to graph.verticesNumber – 1
IF graph.weight[u][v] > 0 and not Contains(lineVisited, pair(u, v))
THEN
arrowLine = Get ArrowLine between vertices at position u and v
line = Line drew based on coordinates of arrowLine
Animate the line
Remove line from GUI
Add the value pair (u, v) to lineVisited

03/08/2024 17 / 22
7. DFS
IF visited[v] is False ENDIF
THEN ENDFOR
visited[v] = true IF v >= graph.verticesNumber – 1
Push(S, u) THEN
Push(S, v) Animate the color of ellipse
Change color of arrowLine to at position u
LightPink to Blue
Animate the color of ellipse at ENDIF
position v ENDWHILE
and u to Black ENDPROCEDURE
BREAK
ELSE
IF Contains(S, v) is Exist
THEN
Change color of arrowLine
to
DarkGray
ELSE
Change color of arrowLine
to 03/08/2024 18
BlueViolet
8. Đọc file
Class MainWindow CALL FileManager.ReadFiles(weightFileName,
PROCEDURE ReadFile_Click(sender, e) vertexFileName , vertexPointFileName)
fileNames = Select files want read CALL guiNodeManager.DrawNodes()
DECLARE vertexFileName, weightFileName, CALL guiLineManager.DrawArrowLines()
vertexPointFileName: STRING ENDPROCEDURE
foreach fileName in fileNames
IF Contains(fileName, “V.txt”) THEN Class FileManager
vertexFileName = fileName PROCEDURE ReadFiles(weightFileName ,
ELSE vertexFileName , vertexPointFileName)
IF Contains(fileName, “M.txt”) matrixFileLines = Read file weightFileName
THEN vertexFileLines = Read file vertexFileName
weightFileName = vertexPointLines = Read file vertexPointFileName
fileName
Convert maxtrixFileLines, vertexFileLines and
ENDIF
vertexPointLines to graph
ELSE
IF Contains(fileName, ENDPROCEDURE
“V.txt”) THEN
vertexPointFileName=
fileName
03/08/2024
ENDIF 19
ENDIF
8. Đọc file
Class GuiNodeManager Class GuiLineManager
PROCEDURE DrawNodes() PROCEDURE DrawArrowLines()
FOR i = 0 to graph.verticesNumber - 1 FOR i = 0 to graph.verticesNumber - 1
tdx = Get Item1 from vertexPoints[i] FOR j = 0 to graph.verticesNumber - 1
tdy = Get Item2 from vertexPoints[i] IF graph.weight[i, j] > 0
vertexName = graph.vertIndexToName[i] THEN
CALL DrawNode(tdx, tdy, vertexName) nodeStart =
ENDFOR graph.vertIndexToName[i]
ENDPROCEDURE nodeEnd =
graph.vertIndexToName[j]
tdx1 = Get Item1 from
vertexPoints[i]
tdy1 = Get Item2 from
vertexPoints[i]
tdx2 = Get Item1 from
vertexPoints[j]
tdy2 = Get Item2 from
vertexPoints[j]
CALL DrawArrowLine(nodeStart,
03/08/2024 20
nodeEnd,
9. Ghi file
Class MainWindow Class FileManager
PROCEDURE WriteFile_Click(sender, e) PROCEDURE WriteFiles(graph, fileName)
fileName = Random file name matrixFileName = fileName + “_M.txt”
CALL FileManager.WriteFiles(graph, vertexFileName = fileName + “_V.txt”
fileName) vertexPointFileName = fileName + “_T.txt”
ENDPROCEDURE
Write graph.weight to maxtrixFileName
Write graph.vertIndexToName to vertexFileName
Write graph.vertexPoints to vertexPointFileName
ENDPROCEDURE

03/08/2024 21
Xin cảm ơn

03/08/2024 22 / 22

You might also like