このブログは、旧・はてなダイアリー「檜山正幸のキマイラ飼育記 メモ編」(http://d.hatena.ne.jp/m-hiyama-memo/)のデータを移行・保存したものであり、今後(2019年1月以降)更新の予定はありません。

今後の更新は、新しいブログ http://m-hiyama-memo.hatenablog.com/ で行います。

有向グラフの例を3つ

コードになにか問題があれば、ここに追記します。

/* Graph1.js */

var Graph1 = {
};

Graph1.isVertex = function(x) {
  return (x === 0 || x === 1 || x === 2);
};

Graph1.isEdge = function(x) {
  return false;
};

Graph1.eqVertex = function(a, b) {
  if (!Graph1.isVertex(a) || !Graph1.isVertex(b))
    throw "not a vertex";
  return a === b;
};

Graph1.eqEdge = function(e, f) {
  throw "not an edge";
};

Graph1.src = function(e) {
  throw "not an edge";
};

Graph1.trg = function(e) {
  throw "not an edge";
};

Graph1.vertex = function(n) {
  if (!Graph1.isVertex(n)) throw "invalid argument";
  return n;
};

Graph1.edge = function() {
  throw "cannot create";
};
</pre>

<pre class="code">
/* Graph2.js */

var Graph2 = {
};

Graph2.isVertex = function(x) {
  return (x === 0 || x === 1 || x === 2);
};

Graph2.isEdge = function(x) {
  return (x === "a" || x === "b");
};

Graph2.eqVertex = function(a, b) {
  if (!Graph2.isVertex(a) || !Graph2.isVertex(b))
    throw "not a vertex";
  return a === b;
};

Graph2.eqEdge = function(e, f) {
  if (!Graph2.isEdge(e) || !Graph2.isEdge(f))
    throw "not an edge";
  return e === f;
};

Graph2.src = function(e) {
  if (!Graph2.isEdge(e))
    throw "not an edge";
  switch (e) {
  case "a" : return 0;
  case "b" : return 1;
  default:
    throw "unbelievable error";
  }
};

Graph2.trg = function(e) {
  if (!Graph2.isEdge(e))
    throw "not an edge";
  switch (e) {
  case "a" : return 1;
  case "b" : return 2;
  default:
    throw "unbelievable error";
  }
};

Graph2.vertex = function(n) {
  if (!Graph2.isVertex(n)) throw "invalid argument";
  return n;
};

Graph2.edge = function(s) {
  if (!Graph2.isEdge(s)) throw "invalid argument";
  return s;
};
</pre>

<pre class="code">
/* Graph3.js */

var Graph3 = {
};

Graph3.isVertex = function(x) {
  return (x === 0 || x === 1 || x === 2);
};

Graph3.isEdge = function(x) {
  return (x === "a" || x === "b" || x === "c" || x === "d");
};

Graph3.eqVertex = function(x, y) {
  if (!Graph3.isVertex(x) || !Graph3.isVertex(y))
    throw "not a vertex";
  return x === y;
};

Graph3.eqEdge = function(x, y) {
  if (!Graph3.isEdge(x) || !Graph3.isEdge(y))
    throw "not an edge";
  return x === y;
};

Graph3.src = function(e) {
  if (!Graph3.isEdge(e))
    throw "not an edge";
  switch (e) {
  case "a" : return 0;
  case "b" : return 1;
  case "c" : return 1;
  case "d" : return 0;
  default:
    throw "unbelievable error";
  }
};
Graph3.trg = function(e) {
  if (!Graph3.isEdge(e))
    throw "not an edge";
  switch (e) {
  case "a" : return 1;
  case "b" : return 2;
  case "c" : return 2;
  case "d" : return 2;
  default:
    throw "unbelievable error";
  }
};

Graph3.vertex = function(n) {
  if (!Graph3.isVertex(n)) throw "invalid argument";
  return n;
};

Graph3.edge = function(s) {
  if (!Graph3.isEdge(s)) throw "invalid argument";
  return s;
};