Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/DirectedEdge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class DirectedEdge extends Edge {
* @param vertexOne
* @param vertexTwo
*/
public constructor(vertexOne: IVertex, vertexTwo: IVertex) {
super(vertexOne, vertexTwo);
public constructor(vertexOne: IVertex, vertexTwo: IVertex, name: string = undefined, weightLabel: string = undefined, isDirected:boolean = true) {
super(vertexOne, vertexTwo, name, weightLabel, isDirected);
}
}
33 changes: 25 additions & 8 deletions src/main/DirectedGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {UndirectedGraph} from "./UndirectedGraph";
import {SccBuilder} from "../algorithms/SccBuilder";
import {IGraph} from "../types/IGraph";
import {IEdge} from "../types/IEdge";
import {forEach} from "lodash";

/**
* Directed graph
Expand All @@ -16,8 +17,8 @@ export class DirectedGraph extends Graph<Vertex, DirectedEdge> {
/**
* @constructor
*/
public constructor() {
super(true);
public constructor(directed: boolean = true) {
super(directed);
}

/**
Expand All @@ -26,8 +27,24 @@ export class DirectedGraph extends Graph<Vertex, DirectedEdge> {
* @param vertexTwo
* @returns {DirectedEdge}
*/
public getEdge(vertexOne: IVertex, vertexTwo: IVertex): DirectedEdge[] {
return this.edges.filter(a => a.vertexTwo.equals(vertexTwo) && a.vertexOne.equals(vertexOne));
public getEdge(vertexOne: IVertex, vertexTwo: IVertex): DirectedEdge[]{

let e:DirectedEdge;

let i:number=0;
let result:DirectedEdge[] =[];

for (i=0; i<this.edges.length; i++)
{
if (this.edges[i].vertexOne.name === vertexOne.name && this.edges[i].vertexTwo.name ===vertexTwo.name)
{
result.push(new DirectedEdge(this.edges[i].vertexOne, this.edges[i].vertexTwo, this.edges[i].name, this.edges[i].weightLabel, this.edges[i].isDirected));
}
}

// return this.edges.filter(a => a.vertexTwo.name === vertexTwo.name && a.vertexOne.name == vertexOne.name);

return result;
}

public buildSCC(): IGraph<IVertex, IEdge>[] {
Expand All @@ -39,13 +56,13 @@ export class DirectedGraph extends Graph<Vertex, DirectedEdge> {
* @returns {DirectedGraph}
*/
public clone(): DirectedGraph {
const clone = new DirectedGraph();
const clone = new DirectedGraph(true);
this.vertices.forEach(v => clone.addVertex(v.clone()));
for (const edge of this.edges)
{
const v1 = clone.vertices.filter(edge.vertexOne.equals)[0]; //Single?
const v2 = clone.vertices.filter(edge.vertexTwo.equals)[0];
clone.addEdge(new DirectedEdge(v1, v2));
// const v1 = clone.vertices.filter(edge.vertexOne.equals)[0]; //Single?
// const v2 = clone.vertices.filter(edge.vertexTwo.equals)[0];
clone.addEdge(new DirectedEdge(edge.vertexOne, edge.vertexTwo, edge.name, edge.weightLabel, edge.isDirected));
}

return clone;
Expand Down
4 changes: 2 additions & 2 deletions src/main/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {UndirectedGraph} from "./UndirectedGraph";

/** @classdesc
* Graph implementation of the IGraph interface */
export class Graph<T extends Vertex, K extends Edge> implements IGraph<T,K> {
export class Graph<T extends Vertex, K extends IEdge> implements IGraph<T,K> {
/** @property
* @private
* Mark shows whether graph edges are directed or not */
Expand Down Expand Up @@ -346,7 +346,7 @@ export class Graph<T extends Vertex, K extends Edge> implements IGraph<T,K> {
* Get vertex's degree
*/
public getVertexDegree(vertex: Vertex): number {
return this.edges.filter((e: Edge) => (e.vertexOne === vertex) || (e.vertexTwo === vertex)).length;
return this.edges.filter((e: IEdge) => (e.vertexOne === vertex) || (e.vertexTwo === vertex)).length;
}

/**
Expand Down