forked from Nisiyama-Suzune/LMR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d73b148
commit 010edcb
Showing
7 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
template <int MAXN = 100000, int MAXM = 100000> | ||
struct chordal_graph { | ||
int n; edge_list <MAXN, MAXM> e; | ||
int id[MAXN], seq[MAXN]; | ||
void init () { | ||
struct point { | ||
int lab, u; | ||
point (int lab = 0, int u = 0) : lab (lab), u (u) {} | ||
bool operator < (const point &a) const { return lab < a.lab; } }; | ||
std::fill (id, id + n, -1); | ||
static int label[MAXN]; std::fill (label, label + n, 0); | ||
std::priority_queue <point> q; | ||
for (int i = 0; i < n; ++i) q.push (point (0, i)); | ||
for (int i = n - 1; i >= 0; --i) { | ||
for (; ~id[q.top ().u]; ) q.pop (); | ||
int u = q.top (); q.pop (); id[u] = i; | ||
for (int j = e.begin[u]; ~j; j = e.next[j]) | ||
if (v = e.dest[j], !~id[v]) ++label[v], q.push (point (label[v], v)); } | ||
for (int i = 0; i < n; ++i) seq[id[i]] = i; } | ||
bool is_chordal () { | ||
static int vis[MAXN], q[MAXN]; std::fill (vis, vis + n, 0); | ||
for (int i = n - 1; i >= 0; --i) { | ||
int u = seq[i], t = 0, v; | ||
for (int j = e.begin[u]; ~j; j = e.next[j]) | ||
if (v = e.dest[j], id[v] > id[u]) q[t++] = v; | ||
if (!t) continue; w = q[0]; | ||
for (int j = 0; j < t; ++j) if (id[q[j]] < id[w]) w = q[j]; | ||
for (int j = e.begin[w]; ~j; j = e.next[j]) vis[e.dest[j]] = i; | ||
for (int j = 0; j < t; ++j) if (q[j] != w && vis[q[j]] != i) return 0; | ||
} | ||
return 1; } | ||
int min_color () { | ||
int res = 0; | ||
static int vis[MAXN], c[MAXN]; | ||
std::fill (vis, vis + n, 0); | ||
std::fill (c, c + n, n); | ||
for (int i = n - 1; i >= 0; --i) { | ||
int u = seq[i]; | ||
for (int j = e.begin[u]; ~j; j = e.next[j]) vis[c[e.dest[j]]] = i; | ||
int k; for (k = 0; vis[k] == i; ++k); | ||
c[u] = k; res = std::max (res, k + 1); | ||
} | ||
return res; } }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
A chordal graph is one in which all cycles of four or more vertices have a chord, which is an edge that is not part of the cycle but connects two vertices of the cycle. | ||
|
||
A perfect elimination ordering in a graph is an ordering of the vertices of the graph such that, for each vertex $v$, $v$ and the neighbors of $v$ that occur after $v$ in the order form a clique. A graph is chordal if and only if it has a perfect elimination ordering. One application of perfect elimination orderings is finding a maximum clique of a chordal graph in polynomial-time, while the same problem for general graphs is NP-complete. More generally, a chordal graph can have only linearly many maximal cliques, while non-chordal graphs may have exponentially many. To list all maximal cliques of a chordal graph, simply find a perfect elimination ordering, form a clique for each vertex $v$ together with the neighbors of $v$ that are later than $v$ in the perfect elimination ordering, and test whether each of the resulting cliques is maximal. | ||
|
||
The largest maximal clique is a maximum clique, and, as chordal graphs are perfect, the size of this clique equals the chromatic number of the chordal graph. Chordal graphs are perfectly orderable: an optimal coloring may be obtained by applying a greedy coloring algorithm to the vertices in the reverse of a perfect elimination ordering. | ||
|
||
In any graph, a vertex separator is a set of vertices the removal of which leaves the remaining graph disconnected; a separator is minimal if it has no proper subset that is also a separator. Chordal graphs are graphs in which each minimal separator is a clique. | ||
|
||
Usage: | ||
\begin{enumerate} | ||
\item Set \texttt{n} and \texttt{e}. | ||
\item Call \texttt{init} to obtain the perfect elimination ordering in \texttt{seq}. | ||
\item Use \texttt{is\_chordal} to test whether the graph is chordal. | ||
\item Use \texttt{min\_color} to obtain the size of the maximum clique (and the chromatic number). | ||
\end{enumerate} | ||
|
||
\lstinputlisting{src/graph/characteristic/chordal-graph.cpp} |