Skip to content

Commit 5e8b47d

Browse files
committed
Adding Graph chapter
1 parent 14c0dac commit 5e8b47d

File tree

4 files changed

+178
-56
lines changed

4 files changed

+178
-56
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
class Graph {
2+
constructor(vertices = [], edges = 0) {
3+
this.vertices = vertices;
4+
this.adjencency = this.fillAdjencencyArray();
5+
this.edges = edges;
6+
}
7+
8+
fillAdjencencyArray() {
9+
let arr = [];
10+
for (let i = 0; i < this.vertices.length; i++) {
11+
arr[i] = [];
12+
for (let j = 0; j < this.vertices.length; j++) {
13+
arr[i].push(0);
14+
}
15+
}
16+
return arr;
17+
}
18+
19+
addEdge(v,w) {
20+
const a = this.vertices.indexOf(v);
21+
const b = this.vertices.indexOf(w);
22+
23+
this.edges++;
24+
25+
if(this.adjencency[a][b] === 1 ) {
26+
this.adjencency[a][b]++;
27+
return;
28+
}
29+
30+
this.adjencency[a][b] = 1;
31+
this.adjencency[b][a] = 1;
32+
}
33+
34+
showGraph() {
35+
let val = ' -> ';
36+
for (const v in this.vertices) {
37+
val += `${this.vertices[v]} `
38+
}
39+
console.log(val);
40+
for (const v in this.adjencency) {
41+
let value = `${this.vertices[v]} -> `;
42+
for (const w in this.adjencency[v]) {
43+
if (this.adjencency[v][w] !== undefined) {
44+
value += `${this.adjencency[v][w]} `
45+
}
46+
}
47+
console.log(value);
48+
}
49+
}
50+
51+
getNumOfVertices() {
52+
return this.vertices.length;
53+
}
54+
55+
getNumOfEdges() {
56+
return this.edges;
57+
}
58+
59+
getValancy(vertex) {
60+
const vIndex = this.vertices.indexOf(vertex);
61+
let valency = 0;
62+
for (var i = 0; i < this.adjencency.length; i++) {
63+
valency += (this.adjencency[i][vIndex] !== 0) ? this.adjencency[i][vIndex] : 0;
64+
}
65+
let valency2 = 0;
66+
for (var i = 0; i < this.adjencency.length; i++) {
67+
valency2 += (this.adjencency[vIndex][i] !== 0) ? this.adjencency[vIndex][i] : 0;
68+
}
69+
return (valency > valency2) ? valency : valency2;
70+
}
71+
72+
}
73+
74+
// Implementation
75+
// #############################################
76+
const vertices = ['a','b','c','d'];
77+
const g = new Graph(vertices);
78+
g.addEdge('a','b');
79+
g.addEdge('a','d');
80+
g.addEdge('b','c');
81+
g.addEdge('b','d');
82+
g.addEdge('c','b');
83+
g.addEdge('d','d');
84+
g.showGraph();
85+
console.log();
86+
console.log(`Number of edges: ${g.getNumOfEdges()}`);
87+
console.log();
88+
for (let i = 0; i < vertices.length; i++) {
89+
console.log(`Valency of ${vertices[i]}: ${g.getValancy(vertices[i])}`);
90+
}
91+
console.log();

11-chapter-2-adjecency-list-Graphs.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
class Graph {
2+
constructor(vertices = [], edges = 0) {
3+
this.vertices = vertices;
4+
this.adjencency = this.fillAdjencencyList();
5+
this.edges = edges;
6+
this.marked = new Array(vertices).fill(false);
7+
}
8+
9+
fillAdjencencyList() {
10+
let arr = [];
11+
for (let i = 0; i < this.vertices; i++) {
12+
arr[i] = [];
13+
}
14+
return arr;
15+
}
16+
17+
addEdge(v,w) {
18+
if (this.adjencency[v].indexOf(w) === -1 && this.adjencency[w].indexOf(v) === -1) {
19+
this.adjencency[v].push(w);
20+
this.adjencency[w].push(v);
21+
this.edges++;
22+
}
23+
}
24+
25+
showGraph() {
26+
let val = '';
27+
for (const v in this.adjencency) {
28+
let value = `${v} -> `;
29+
for (const w in this.adjencency[v]) {
30+
if (this.adjencency[v][w] !== undefined) {
31+
value += `${this.adjencency[v][w]} `
32+
}
33+
}
34+
console.log(value);
35+
}
36+
}
37+
38+
dfs(node) {
39+
if (this.marked[node]) {
40+
return;
41+
}
42+
console.log(`visited node at: ${node}`);
43+
this.marked[node] = true;
44+
if (this.adjencency[node] !== undefined) {
45+
for (const w in this.adjencency[node]) {
46+
this.dfs(this.adjencency[node][w]);
47+
}
48+
}
49+
}
50+
51+
bfs(node) {
52+
let queue = [node];
53+
while (queue.length > 0) {
54+
let v = queue.shift();
55+
this.marked[v] = true;
56+
console.log(`visited node at: ${v}`);
57+
for (const w in this.adjencency[v]) {
58+
let n = this.adjencency[v][w];
59+
if (!this.marked[n]) {
60+
queue.push(this.adjencency[v][w]);
61+
}
62+
}
63+
}
64+
}
65+
}
66+
67+
// Implementation
68+
// #############################################
69+
const g = new Graph(5);
70+
g.addEdge(0,1);
71+
g.addEdge(0,2);
72+
g.addEdge(1,3);
73+
g.addEdge(2,4);
74+
g.showGraph();
75+
let t0 = new Date().getTime();
76+
g.dfs(0);
77+
let t1 = new Date().getTime();
78+
console.log("Call to dfs took " + (t1 - t0) + " milliseconds.")
79+
80+
g.marked = new Array(g.vertices).fill(false);
81+
console.log();
82+
let t2 = new Date().getTime();
83+
g.bfs(0);
84+
let t3 = new Date().getTime();
85+
console.log("Call to bfs took " + (t3 - t2) + " milliseconds.")

11-chapter-Graphs.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ The purpose of this repo is to update the exercises to ES6 standards and to corr
2222
| 9.- | [Hashing](./08-chapter-Hashing.js) | 2 | `Hashing` is a common technique for storing data in such a way that the data can be inserted and retrieved very quickly. Hashing uses a data structure called a hash table. Although hash tables provide fast insertion, deletion, and retrieval, they perform poorly for operations that involve searching.
2323
| 10.- | [Set](./09-chapter-Set.js) | 4 | A `Set` is a collection of unique elements. The elements of a set are called members. The two most important properties of sets are that the members of a set are unordered and that no member can occur in a set more than once.
2424
| 11.- | [Binary Trees and Binary Search Trees](./10-chapter-Binary-Trees.js) | 4 | `Binary trees` are chosen over other more primary data structures because you can search a binary tree very quickly (as opposed to a linked list, for example) and you can quickly insert and delete data from a binary tree (as opposed to an array).
25-
| 12.- | [Graphs and Graphs Algorithms](./11-chapter-Graphs.js) | | A graph consists of a set of vertices and a set of edges. Think of a map of a US state. Each town is connected with other towns via some type of road. A map is a type of graph where each town is a vertex, and a road that connects two towns is an edge. Edges are defined as a pair (v1, v2), where v1 and v2 are two vertices in a graph
25+
| 12.- | [Simple Graphs](./11-chapter-1-MatrixAdjencency-Graphs.js) | 2 | A graph consists of a set of vertices and a set of edges. A map is a type of graph where each town is a vertex, and a road that connects two towns is an edge. Edges are defined as a pair (v1, v2), where v1 and v2 are two vertices in a graph
26+
| 13.- | [Complete Graphs](./11-chapter-2-ArrayListAdjecency-Graphs.js) | 2 | A complete graph is where all vertices are conected with each other.
2627

2728

2829
### To run the examples we need the following:

0 commit comments

Comments
 (0)