-
Notifications
You must be signed in to change notification settings - Fork 0
Implement Mayer's algorithm #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
denistakeda
wants to merge
2
commits into
master
Choose a base branch
from
mayers-algorithm
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,121 @@ | ||
export interface DirectedEdge { | ||
readonly from: number | ||
readonly to: number | ||
readonly weight: number | ||
} | ||
|
||
export interface EdgeWeightedDigraph { | ||
v(): number | ||
adj(v: number): DirectedEdge[] | ||
} | ||
|
||
export class MayersGraph<T> implements EdgeWeightedDigraph { | ||
constructor(private readonly oldS: T[], private readonly newS: T[]) { | ||
|
||
} | ||
|
||
public v(): number { | ||
return (this.oldS.length + 1) * (this.newS.length + 1) | ||
} | ||
|
||
// No need to store the adjacency list, we can generate it on the flight | ||
public adj(v: number): DirectedEdge[] { | ||
const [ newId, oldId ] = this.toCoord(v) | ||
|
||
let result: DirectedEdge[] = [] | ||
if (oldId < this.oldS.length && newId < this.newS.length && this.oldS[oldId] === this.newS[newId]) { | ||
result.push({ | ||
from: v, | ||
to: (newId + 1) * (this.oldS.length + 1) + oldId + 1, | ||
weight: 0, | ||
}) | ||
return result | ||
} | ||
if (oldId < this.oldS.length) | ||
result.push({ from: v, to: v + 1, weight: 1 }) | ||
if (newId < this.newS.length) | ||
result.push({ from: v, to: v + this.oldS.length + 1, weight: 1 }) | ||
return result | ||
} | ||
|
||
public toCoord(v: number): [number, number] { | ||
return [Math.floor(v / (this.oldS.length + 1)), v % (this.oldS.length + 1)] | ||
} | ||
} | ||
|
||
|
||
export function topologicalOrder(g: EdgeWeightedDigraph, s: number): number[] { | ||
const order: number[] = [] | ||
const marked: boolean[] = [] | ||
// TODO: use the explicit stack instead of recursion | ||
const dfs = (g: EdgeWeightedDigraph, v: number) => { | ||
marked[v] = true | ||
for (let e of g.adj(v)) | ||
if (!marked[e.to]) | ||
dfs(g, e.to) | ||
|
||
order.unshift(v) | ||
} | ||
dfs(g, s) | ||
return order | ||
} | ||
|
||
// -- Shortest path -- | ||
|
||
export function shortestPath(g: EdgeWeightedDigraph, s: number, d: number): DirectedEdge[] { | ||
const edgeTo: DirectedEdge[] = [] | ||
const distTo: number[] = Array(g.v()).fill(Number.MAX_SAFE_INTEGER) | ||
|
||
distTo[s] = 0 | ||
for (let v of topologicalOrder(g, s)) { | ||
relax(g, v, edgeTo, distTo) | ||
} | ||
return pathTo(d, edgeTo) | ||
} | ||
|
||
function relax(g: EdgeWeightedDigraph, v: number, edgeTo: DirectedEdge[], distTo: number[]) { | ||
for (let e of g.adj(v)) { | ||
const w = e.to | ||
if (distTo[w] > distTo[v] + e.weight) { | ||
distTo[w] = distTo[v] + e.weight | ||
edgeTo[w] = e | ||
} | ||
} | ||
} | ||
|
||
function pathTo(d: number, edgeTo: DirectedEdge[]): DirectedEdge[] { | ||
const path: DirectedEdge[] = [] | ||
for (let e = edgeTo[d]; e !== undefined; e = edgeTo[e.from]) | ||
path.unshift(e) | ||
return path | ||
} | ||
|
||
// -------------------- | ||
|
||
// -- Mayer's algorithm -- | ||
|
||
interface InsertAction<T> { | ||
kind: 'insert', | ||
value: T, | ||
} | ||
interface DeleteAction<T> { | ||
kind: 'delete', | ||
value: T, | ||
} | ||
interface SkipAction<T> { | ||
kind: 'skip', | ||
value: T, | ||
} | ||
type Action<T> = InsertAction<T> | DeleteAction<T> | SkipAction<T> | ||
|
||
export function mayers<T>(oldArr: T[], newArr: T[]): Action<T>[] { | ||
const g = new MayersGraph(oldArr, newArr) | ||
const path = shortestPath(g, 0, g.v() - 1) | ||
return path.map(edge => { | ||
const [y1, x1] = g.toCoord(edge.from) | ||
const [y2, x2] = g.toCoord(edge.to) | ||
if (y2 > y1 && x2 > x1) return { kind: 'skip', value: newArr[y1]} | ||
else if (y2 > y1) return { kind: 'insert', value: newArr[y1]} | ||
else return { kind: 'delete', value: oldArr[x1] } | ||
}) | ||
} |
This file contains hidden or 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 hidden or 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,57 @@ | ||
import { MayersGraph, topologicalOrder, shortestPath, mayers } from '../src/Graph' | ||
|
||
const graph = new MayersGraph<String>('bac'.split(''), 'cbcb'.split('')) | ||
const smallGraph = new MayersGraph<String>(['a'], ['b']) | ||
|
||
describe('DoubleStringGraph', () => { | ||
it('should calculate the graph size', () => { | ||
expect(graph.v()).toBe(20) | ||
}) | ||
|
||
it('should return the right adjacency lists', () => { | ||
expect(graph.adj(0)).toEqual([ | ||
{ from: 0, to: 1, weight: 1 }, | ||
{ from: 0, to: 4, weight: 1 } | ||
]) | ||
expect(graph.adj(2)).toEqual([ | ||
{ from: 2, to: 7, weight: 0 } | ||
]) | ||
expect(graph.adj(3)).toEqual([ | ||
{ from: 3, to: 7, weight: 1 } | ||
]) | ||
expect(graph.adj(16)).toEqual([ | ||
{ from: 16, to: 17, weight: 1 } | ||
]) | ||
expect(graph.adj(19)).toEqual([]) | ||
}) | ||
}) | ||
|
||
describe('topologicalOrder', () => { | ||
it('should sort small graph in a topological order', () => { | ||
expect(topologicalOrder(smallGraph, 0)).toEqual([0, 2, 1, 3]) | ||
}) | ||
}) | ||
|
||
describe('shortestPath', () => { | ||
it('should provide a shortest path', ()=> { | ||
expect(shortestPath(graph, 0, 19)).toEqual([ | ||
{from: 0, to: 4, weight: 1}, | ||
{from: 4, to: 9, weight: 0}, | ||
{from: 9, to: 10, weight: 1}, | ||
{from: 10, to: 15, weight: 0}, | ||
{from: 15, to: 19, weight: 1} | ||
]) | ||
}) | ||
}) | ||
|
||
describe('mayers', () => { | ||
it('should return the list of mutations', () => { | ||
expect(mayers('bac'.split(''), 'cbcb'.split(''))).toEqual([ | ||
{kind: "insert", value: "c"}, | ||
{kind: "skip", value: "b"}, | ||
{kind: "delete", value: "a"}, | ||
{kind: "skip", value: "c"}, | ||
{kind: "insert", value: "b"} | ||
]) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@denistakeda it's hard to understand what is
v
. Is it vertex id?