Skip to content

Commit

Permalink
Add code documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
beejjorgensen committed Jan 4, 2018
1 parent 275b68d commit 57c64af
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
6 changes: 6 additions & 0 deletions projects/graph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,9 @@ This is a necessary step in figuring out the connected components.
It's likely the random graphs have multiple connected components. Choose
a random color for the edges of connected component.
## Phase 6: Add a UI Button to Generate a new Graph
Instead of hitting reload, it would be nice to have a button that you
could press to generate a new random graph and show the connected
components.
23 changes: 23 additions & 0 deletions projects/graph/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import React, { Component } from 'react';
import { Graph } from './graph';
import './App.css';

// Define the size of the random graph
const xCount = 5;
const yCount = 5;
const boxSize = 150;
const probability = 0.6;

// Figure out the canvas size
const canvasWidth = boxSize * xCount;
const canvasHeight = boxSize * yCount;
const radius = boxSize / 8;
Expand All @@ -15,14 +17,23 @@ const radius = boxSize / 8;
* GraphView
*/
class GraphView extends Component {
/**
* On mount
*/
componentDidMount() {
this.updateCanvasConnectedComponents();
}

/**
* On state update
*/
componentDidUpdate() {
this.updateCanvasConnectedComponents();
}

/**
* Draw the given verts
*/
drawVerts(vertexes, color='blue', clear=true) {
let canvas = this.refs.canvas;
let ctx = canvas.getContext('2d');
Expand Down Expand Up @@ -67,12 +78,18 @@ class GraphView extends Component {
}
}

/**
* Draw the entire graph
*/
updateCanvasEntireGraph() {
const g = this.props.graph;
this.drawVerts(g.vertexes);
//g.dump();
}

/**
* Draw the connected components
*/
updateCanvasConnectedComponents() {
function randomHexColor() {
let color = ((Math.random() * 240)|0).toString(16);
Expand All @@ -98,6 +115,9 @@ class GraphView extends Component {
}
}

/**
* Render
*/
render() {
return <canvas ref="canvas" width={canvasWidth} height={canvasHeight}></canvas>;
}
Expand All @@ -119,6 +139,9 @@ class App extends Component {
this.state.graph.randomize(xCount, yCount, boxSize, probability);
}

/**
* Handle the button press
*/
onButton() {
const state = {
graph: new Graph()
Expand Down
21 changes: 21 additions & 0 deletions projects/graph/src/graph.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
/**
* Edge
*/
export class Edge {
constructor(destination, weight=1) {
this.destination = destination;
this.weight = weight;
}
}

/**
* Vertex
*/
export class Vertex {
constructor(value='vertex') {
this.value = value;
this.edges = [];
}
}

/**
* Graph
*/
export class Graph {
constructor() {
this.vertexes = [];
}

/**
* Create a random graph
*/
randomize(width, height, pxBox, probability=0.6) {
// Helper function to set up two-way edges
function connectVerts(v0, v1) {
Expand Down Expand Up @@ -80,6 +92,9 @@ export class Graph {
}
}

/**
* Dump graph data to the console
*/
dump() {
let s;

Expand All @@ -97,6 +112,9 @@ export class Graph {
}
}

/**
* BFS
*/
bfs(start, reset=true) {
const component = [];
const queue = [];
Expand Down Expand Up @@ -131,6 +149,9 @@ export class Graph {
return component;
}

/**
* Get the connected components
*/
getConnectedComponents() {
const componentsList = [];

Expand Down
21 changes: 21 additions & 0 deletions sprint-challenge/src/routing.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
/**
* Edge class
*/
class Edge {
constructor(destination, weight=1) {
this.destination = destination;
this.weight = weight;
}
}

/**
* Vertex class
*/
class Vertex {
constructor(value='vertex') {
this.value = value;
this.edges = [];
}
}

/**
* Graph class
*/
class Graph {
constructor() {
this.vertexes = [];
}

/**
* Breadth-First search from a starting vertex
*/
bfs(start) {
const queue = [];

Expand Down Expand Up @@ -46,6 +58,11 @@ class Graph {
}
}

/**
* Find a vertex by its value
*
* Return null if the vertex isn't found
*/
findVertex(value) {
for (let v of this.vertexes) {
if (v.value == value) {
Expand All @@ -56,6 +73,10 @@ class Graph {
return null;
}

/**
* Print out the route from the start vert back along the parent
* pointers (set in the previous BFS)
*/
route(start) {
let p = start;
let s = '';
Expand Down

0 comments on commit 57c64af

Please sign in to comment.