File tree 16 files changed +176
-8
lines changed
3-graph-algorithms/1_graph_decomposition
16 files changed +176
-8
lines changed Original file line number Diff line number Diff line change 1
- #Uses python3
2
-
3
1
import sys
4
2
3
+ class Graph :
4
+ def __init__ (self , n , edges ):
5
+ self .adj = [[] for _ in range (n )]
6
+ self .buildAdjacencyList (edges )
7
+
8
+ # Time Complexity: O(|E|)
9
+ # Space Complexity: O(1)
10
+ def buildAdjacencyList (self , edges ):
11
+ self .reset ()
12
+
13
+ for (a , b ) in edges :
14
+ self .adj [a - 1 ].append (b - 1 )
15
+
16
+ def reset (self ):
17
+ self .visited = [False ] * len (self .adj )
18
+ self .removed = [False ] * len (self .adj )
19
+
20
+ def explore (self , v ):
21
+ self .visited [v ] = True
22
+ for a in self .adj [v ]:
23
+ if not self .removed [a ]:
24
+ if self .visited [a ]:
25
+ return 1
26
+ if self .explore (a ) == 1 :
27
+ return 1
28
+
29
+ self .removed [v ] = True
30
+ return 0
31
+
32
+ def DFS (self ):
33
+ self .reset ()
34
+ for v in range (len (self .adj )):
35
+ if (not self .removed [v ]) and self .explore (v ) == 1 :
36
+ return 1
37
+
38
+ return 0
39
+
40
+ def acyclic (n , edges ):
5
41
6
- def acyclic ( adj ):
7
- return 0
42
+ aGraph = Graph ( n , edges )
43
+ return aGraph . DFS ()
8
44
9
45
if __name__ == '__main__' :
10
46
input = sys .stdin .read ()
11
47
data = list (map (int , input .split ()))
12
48
n , m = data [0 :2 ]
13
49
data = data [2 :]
14
50
edges = list (zip (data [0 :(2 * m ):2 ], data [1 :(2 * m ):2 ]))
15
- adj = [[] for _ in range (n )]
16
- for (a , b ) in edges :
17
- adj [a - 1 ].append (b - 1 )
18
- print (acyclic (adj ))
51
+ print (acyclic (n , edges ))
Original file line number Diff line number Diff line change
1
+ 0 0
Original file line number Diff line number Diff line change
1
+ 0
Original file line number Diff line number Diff line change
1
+ 0
Original file line number Diff line number Diff line change
1
+ 4 4
2
+ 1 2
3
+ 4 1
4
+ 2 3
5
+ 3 1
Original file line number Diff line number Diff line change
1
+ 1
Original file line number Diff line number Diff line change
1
+ 1
Original file line number Diff line number Diff line change
1
+ 4 3
2
+ 1 2
3
+ 3 2
4
+ 4 3
Original file line number Diff line number Diff line change
1
+ 0
Original file line number Diff line number Diff line change
1
+ 0
Original file line number Diff line number Diff line change
1
+ 14 13
2
+ 1 2
3
+ 1 3
4
+ 3 5
5
+ 3 4
6
+ 4 6
7
+ 4 7
8
+ 4 8
9
+ 6 8
10
+ 6 9
11
+ 6 10
12
+ 8 11
13
+ 12 13
14
+ 12 14
Original file line number Diff line number Diff line change
1
+ 1
Original file line number Diff line number Diff line change
1
+ 0
Original file line number Diff line number Diff line change
1
+ 100 100
2
+ 27 96
3
+ 6 9
4
+ 81 98
5
+ 21 94
6
+ 22 68
7
+ 76 100
8
+ 8 50
9
+ 38 86
10
+ 71 75
11
+ 32 93
12
+ 16 50
13
+ 71 84
14
+ 6 72
15
+ 22 58
16
+ 7 19
17
+ 19 76
18
+ 44 75
19
+ 24 76
20
+ 31 35
21
+ 11 89
22
+ 42 98
23
+ 63 92
24
+ 37 38
25
+ 20 98
26
+ 45 91
27
+ 23 53
28
+ 37 91
29
+ 76 93
30
+ 67 90
31
+ 12 22
32
+ 43 52
33
+ 23 56
34
+ 67 68
35
+ 1 21
36
+ 17 83
37
+ 63 72
38
+ 30 32
39
+ 7 91
40
+ 50 69
41
+ 38 44
42
+ 55 89
43
+ 15 23
44
+ 11 72
45
+ 28 42
46
+ 22 69
47
+ 56 79
48
+ 5 83
49
+ 55 73
50
+ 13 72
51
+ 7 93
52
+ 20 54
53
+ 21 55
54
+ 66 89
55
+ 2 91
56
+ 18 88
57
+ 26 64
58
+ 11 61
59
+ 28 59
60
+ 12 86
61
+ 42 95
62
+ 17 82
63
+ 50 66
64
+ 66 99
65
+ 40 71
66
+ 20 40
67
+ 5 66
68
+ 92 95
69
+ 32 46
70
+ 7 36
71
+ 44 94
72
+ 6 31
73
+ 19 67
74
+ 26 57
75
+ 53 84
76
+ 10 68
77
+ 28 74
78
+ 34 94
79
+ 25 61
80
+ 71 88
81
+ 10 89
82
+ 28 52
83
+ 72 79
84
+ 39 73
85
+ 11 80
86
+ 44 79
87
+ 13 77
88
+ 30 96
89
+ 30 53
90
+ 10 39
91
+ 1 90
92
+ 40 91
93
+ 62 71
94
+ 44 54
95
+ 15 17
96
+ 69 74
97
+ 13 67
98
+ 24 69
99
+ 34 96
100
+ 21 50
101
+ 20 91
Original file line number Diff line number Diff line change
1
+ 1
Original file line number Diff line number Diff line change
1
+ 0
You can’t perform that action at this time.
0 commit comments