/
graph_condense.go
executable file
·110 lines (99 loc) · 2.85 KB
/
graph_condense.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (C) 2021 CGI France
//
// This file is part of LINO.
//
// LINO is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LINO is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LINO. If not, see <http://www.gnu.org/licenses/>.
package id
// condenser applies the condensation algorithm
type condenser struct {
graph graph
stack stack
index uint
components []TableList
vindex map[string]uint
vlowlink map[string]uint
vonstack map[string]bool
}
// condense the graph
func (g graph) condense() []TableList {
c := &condenser{
graph: g,
stack: newStack(),
components: []TableList{},
vindex: map[string]uint{},
vlowlink: map[string]uint{},
vonstack: map[string]bool{},
}
c.tarjan()
return c.components
}
func (c *condenser) tarjan() {
for _, v := range c.graph.tabmap {
_, ok := c.vindex[v.Name()]
if !ok {
c.strongconnect(v)
}
}
}
func (c *condenser) strongconnect(v Table) {
// Set the depth index for v to the smallest unused index
c.vindex[v.Name()] = c.index
c.vlowlink[v.Name()] = c.index
c.index++
c.stack.push(v)
c.vonstack[v.Name()] = true
// Consider successors of v
for _, relname := range c.graph.activefrom[v.Name()] {
e := c.graph.relmap[relname]
var w Table
if v.Name() != e.Child().Name() {
w = e.Child()
} else {
w = e.Parent()
}
_, ok := c.vindex[w.Name()]
if !ok {
// Successor w has not yet been visited; recurse on it
c.strongconnect(w)
c.vlowlink[v.Name()] = umin(c.vlowlink[v.Name()], c.vlowlink[w.Name()])
} else if c.vonstack[w.Name()] {
// Successor w is in stack S and hence in the current SCC
// If w is not on stack, then (v, w) is a cross-edge in the DFS tree and must be ignored
// Note: The next line may look odd - but is correct.
// It says w.index not w.lowlink; that is deliberate and from the original paper
c.vlowlink[v.Name()] = umin(c.vlowlink[v.Name()], c.vindex[w.Name()])
}
}
// If v is a root node, pop the stack and generate an SCC
if c.vlowlink[v.Name()] == c.vindex[v.Name()] {
// start a new strongly connected component
tables := []Table{}
for {
w := c.stack.pop()
c.vonstack[w.Name()] = false
// add w to current strongly connected component
tables = append(tables, w)
if w.Name() == v.Name() {
break
}
}
c.components = append(c.components, NewTableList(tables))
}
}
func umin(a, b uint) uint {
if a < b {
return a
}
return b
}