@@ -47,7 +47,7 @@ public BranchResponseDTO.getMemberBranchView getMemberBranchView(User user, Long
47
47
List <Branch > branches = branchQueryAdapter .findAllByTreeHouse (treeHouse ); // 트리하우스 내 모든 브랜치 조회
48
48
Member member = memberQueryAdapter .findByUserAndTreehouse (user , treeHouse ); // 현재 로그인한 트리하우스 멤버
49
49
Long rootId = member .getId (); // 현재 로그인한 트리하우스 멤버의 ID
50
- BranchResponseDTO .ShortestPathResult shortestPathResult = findShortestDistance (branches , rootId , targetMemberId ); // 최단 거리 계산
50
+ BranchResponseDTO .ShortestPathResult shortestPathResult = BranchUtil . findShortestDistance (branches , rootId , targetMemberId ); // 최단 거리 계산
51
51
52
52
// 최단 거리 결과를 이용해 브랜치 뷰 생성
53
53
// Node 정보 생성
@@ -91,73 +91,4 @@ public BranchResponseDTO.getCompleteBranchView getCompleteBranchView(User user,
91
91
// 브랜치 뷰 생성
92
92
return BranchMapper .toCompleteBranchView (nodes , links );
93
93
}
94
-
95
- /**
96
- * 두 멤버 사이의 가장 짧은 거리를 계산하여 정수값으로 니다.
97
- * @param treeHouse
98
- * @param rootId // 시작 멤버 ID
99
- * @param leafId // 끝(대상) 멤버 ID
100
- * @return 두 멤버 사이의 가장 짧은 거리
101
- */
102
-
103
- public Integer calculateBranchDegree (TreeHouse treeHouse , Long rootId , Long leafId ) {
104
- // 두 멤버 사이의 모든 Branch 엔티티를 찾습니다.
105
- List <Branch > branches = branchQueryAdapter .findAllByTreeHouse (treeHouse );
106
-
107
- // Branch 목록을 사용하여 최단 거리를 계산합니다.
108
- int shortestDistance = findShortestDistance (branches , rootId , leafId ).getDistance ();
109
-
110
- return shortestDistance ;
111
- }
112
-
113
- /**
114
- * BFS 알고리즘을 이용해 두 멤버 사이의 가장 짧은 거리를 계산합니다.
115
- * @param branches
116
- * @param startMemberId
117
- * @param endMemberId
118
- * @return 두 멤버 사이의 가장 짧은 거리
119
- */
120
-
121
- public BranchResponseDTO .ShortestPathResult findShortestDistance (List <Branch > branches , Long startMemberId , Long endMemberId ) {
122
- Map <Long , List <Long >> adjacencyList = new HashMap <>();
123
- Map <Long , Long > prev = new HashMap <>();
124
- Set <Long > visited = new HashSet <>();
125
- Queue <Long > queue = new LinkedList <>();
126
-
127
- // 각 멤버 ID를 기준으로 연결된 Branch를 매핑
128
- for (Branch branch : branches ) {
129
- adjacencyList .computeIfAbsent (branch .getRoot ().getId (), k -> new ArrayList <>()).add (branch .getLeaf ().getId ());
130
- adjacencyList .computeIfAbsent (branch .getLeaf ().getId (), k -> new ArrayList <>()).add (branch .getRoot ().getId ());
131
- }
132
-
133
- queue .add (startMemberId );
134
- visited .add (startMemberId );
135
- prev .put (startMemberId , null ); // 시작 노드의 선행자는 없음
136
-
137
- while (!queue .isEmpty ()) {
138
- Long current = queue .poll ();
139
- if (current .equals (endMemberId )) {
140
- break ; // 목표 노드에 도달
141
- }
142
- for (Long neighbor : adjacencyList .getOrDefault (current , Collections .emptyList ())) {
143
- if (!visited .contains (neighbor )) {
144
- visited .add (neighbor );
145
- queue .add (neighbor );
146
- prev .put (neighbor , current );
147
- }
148
- }
149
- }
150
-
151
- // 경로 복원 및 결과 생성
152
- List <Long > path = new ArrayList <>();
153
- Long current = endMemberId ;
154
- while (current != null ) {
155
- path .add (current );
156
- current = prev .get (current );
157
- }
158
- Collections .reverse (path ); // 경로를 역순으로 뒤집어 정상 순서로 만듦
159
-
160
- int distance = path .size () - 1 ; // 거리는 경로의 길이에서 1을 뺀 값
161
- return BranchMapper .toShortestPathResult (distance , path );
162
- }
163
94
}
0 commit comments