Skip to content

Commit 5cb2872

Browse files
committed
again changed path and added criticalConnections
1 parent c35ce5c commit 5cb2872

15 files changed

+95
-252
lines changed

src/bst/problems/bst-max-path-sum.ts renamed to src/bst/bst-max-path-sum.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33

44
function maxPathSum(root: TreeNode | null): number {

src/bst/problems/bst-side-view.ts renamed to src/bst/bst-side-view.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
function rightSideView(root: TreeNode | null): number[] {
44
const result: number[] = [];

src/bst/problems/delete.ts renamed to src/bst/delete.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
function deleteNode(root: TreeNode | null, key: number): TreeNode | null {
44
if (!root) {

src/bst/problems/duplicates-subtrees.ts renamed to src/bst/duplicates-subtrees.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
function findDuplicateSubtrees(root: TreeNode | null): TreeNode[] {
44
const map = new Map<string, number>();

src/bst/problems/find-min.ts renamed to src/bst/find-min.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TreeNode } from "../utilities";
1+
import { TreeNode } from "./utilities";
22

33
function findMin(root: TreeNode | null): number | null {
44
if (!root) {

src/bst/problems/insert.ts renamed to src/bst/insert.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
function insertIntoBST(root: TreeNode | null, val: number): TreeNode | null {
44
if (!root) {

src/bst/problems/is-balanced.ts renamed to src/bst/is-balanced.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
function isBalanced(root: TreeNode | null): boolean {
44
function checkBalance(node: TreeNode | null): number {

src/bst/problems/is-symmetric.ts renamed to src/bst/is-symmetric.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
function isSymmetric(root: TreeNode | null): boolean {
44
if (!root) return true;

src/bst/problems/is-valid-bst.ts renamed to src/bst/is-valid-bst.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
function isValidBST(root: TreeNode | null, min?: number, max?: number): boolean {
44
const currentVal = root?.val;

src/bst/problems/lowest-common-ancestor.ts renamed to src/bst/lowest-common-ancestor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
44
if (root === null || root === p || root === q) {

src/bst/problems/max-depth.ts renamed to src/bst/max-depth.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
export namespace Recursion {
44
function maxDepth(root: TreeNode | null): number {

src/bst/problems/serialize-and-deserialize.ts renamed to src/bst/serialize-and-deserialize.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { printTree, TreeNode } from "../utilities";
1+
import { printTree, TreeNode } from "./utilities";
22

33
function serialize(root: TreeNode): string {
44
const preorderSerialize = (node: TreeNode) => {

src/graph/critical-connections.ts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
const connections = [
3+
[0,1],
4+
[1,2],
5+
[2,0],
6+
[1,3]
7+
]
8+
9+
function criticalConnections(n: number, connections: number[][]): number[][] {
10+
const graph: number[][] = Array.from({ length: n }, () => []);
11+
const result: number[][] = [];
12+
const disc: number[] = new Array(n).fill(-1); // Время открытия вершины
13+
const low: number[] = new Array(n).fill(-1); // Минимальное время посещения
14+
let time = 0;
15+
16+
// Построение графа
17+
for (const [u, v] of connections) {
18+
graph[u].push(v);
19+
graph[v].push(u);
20+
}
21+
22+
// Основная функция DFS
23+
const dfs = (u: number, parent: number) => {
24+
disc[u] = low[u] = time++; // Инициализация времени открытия и low
25+
for (const v of graph[u]) {
26+
if (v === parent) continue; // Игнорируем родительскую вершину
27+
28+
if (disc[v] === -1) { // Если вершина v еще не посещена
29+
dfs(v, u); // Рекурсивный DFS
30+
31+
// Обновляем low[u] на основе дочерней вершины v
32+
low[u] = Math.min(low[u], low[v]);
33+
34+
// Если low[v] > disc[u], то (u, v) — критическое соединение
35+
if (low[v] > disc[u]) {
36+
result.push([u, v]);
37+
}
38+
} else {
39+
// Обратное ребро, обновляем low[u]
40+
low[u] = Math.min(low[u], disc[v]);
41+
}
42+
}
43+
};
44+
45+
// Запуск DFS для всех вершин
46+
for (let i = 0; i < n; i++) {
47+
if (disc[i] === -1) {
48+
dfs(i, -1); // -1 означает, что у начальной вершины нет родителя
49+
}
50+
}
51+
52+
return result;
53+
}
54+
55+
56+
export function criticalConnectionsDBG(){
57+
58+
const tests = [
59+
{
60+
input: { n: 4, connections: [[0, 1], [1, 2], [2, 0], [1, 3]] },
61+
result: [[1, 3]] // Соединение [1, 3] является критическим
62+
},
63+
{
64+
input: { n: 2, connections: [[0, 1]] },
65+
result: [[0, 1]] // Единственное соединение [0, 1] является критическим
66+
},
67+
];
68+
69+
tests.forEach((test, index) => {
70+
const result = criticalConnections(test.input.n, test.input.connections);
71+
const success = JSON.stringify(result) === JSON.stringify(test.result);
72+
if (success) {
73+
console.log(`Test ${index} success`);
74+
} else {
75+
console.log(`Test ${index} fail`);
76+
console.log(`expected: ${JSON.stringify(test.result)}`);
77+
console.log(`got: ${JSON.stringify(result)}`);
78+
}
79+
});
80+
81+
}

0 commit comments

Comments
 (0)