Skip to content

Instantly share code, notes, and snippets.

View zhuangh's full-sized avatar
🎯
Focusing

Hao Zhuang zhuangh

🎯
Focusing
View GitHub Profile
@zhuangh
zhuangh / kruskal.cc
Last active January 31, 2018 00:08
kruskal algorithm in C++
#include <iostream>
template<typename T=int>
class edgeType{
private:
char from;
char to;
T w;
public:
edgeType(char a, char b, int c):from(a),to(b),w(c) {}
@zhuangh
zhuangh / kruskal.py
Created January 30, 2018 00:02
Kruskal method
def union_find(u, arr):
if arr[u] == u:
return u
return union_find(arr[u], arr)
def union(u, v, arr):
arr[union_find(v, arr)] = union_find(u, arr)
def kruskal(nodes, edges):
mst = []
@zhuangh
zhuangh / mst_prim.py
Created January 30, 2018 00:01
prim for MST
def prim(nodes, edges):
conn = {}
for u, v, w in edges:
if u not in conn:
conn[u] = [(w,u,v)]
else:
conn[u].append((w, u, v))
if v not in conn:
conn[v] = [(w,v,u)]
else:
@zhuangh
zhuangh / bellman_ford.py
Created January 29, 2018 08:12
Bellman Ford python version
class Solution:
def bellman_ford(self, K, N, delay, graph, TIMEOUT):
delay[K-1] = 0
for i in range(N):
for u, vs in graph.items():
for v, w in vs.items():
if delay[v] > delay[u] + w:
delay[v] = delay[u] + w
d = max(delay)
@zhuangh
zhuangh / dijkstra.py
Created January 29, 2018 08:10
Dijkstra python version
class Solution:
def dijkstra(self, K, N, delay, graph, TIMEOUT):
Q = set(range(N))
delay[K-1] = 0
hp = []
heapq.heappush(hp, (0, K-1))
while len(hp) > 0:
_, u = heapq.heappop(hp)
for v, w in graph[u].items():
if delay[u] + w < delay[v]:
@zhuangh
zhuangh / dijkstra.cc
Last active January 29, 2018 08:14
Dijkstra's algorithm
using pairType = std::pair<int, int>;
using graphType = std::unordered_map<int, vector<pairType> >;
using heapType = std::priority_queue<pairType, vector<pairType> >;
class Solution {
private:
int dijkstra(const int & K, const int &N, std::vector<int> &delay,
graphType & graph, const int &TIMEOUT ){
int d = -1;
@zhuangh
zhuangh / bellmanFord.cc
Last active February 7, 2018 07:07
bellmanFord algorithm
using pairType = std::pair<int, int>;
using graphType = std::unordered_map<int, vector<pairType> >;
class Solution {
private:
int bellmanFord(const int & K, const int &N, std::vector<int> &delay,
graphType & graph, const int &TIMEOUT ){
int d = -1;
delay[K] = 0;
@zhuangh
zhuangh / lu_ops_test.py
Last active January 18, 2018 22:36
tensorflow lu ops related (test script)
import tensorflow as tf
import numpy as np
w = np.array([[3,-1,-1], [-1,5.0,-1], [1, 0 ,10]])
print(w)
W = tf.constant(w)
l, u, p, q = tf.lu(W)
pinv = tf.matrix_inverse(p)
qinv = tf.matrix_inverse(q)
@zhuangh
zhuangh / lu_ops.cc
Created January 14, 2018 00:33
tensorflow lu ops related
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
@zhuangh
zhuangh / ops.pbtxt
Last active February 5, 2018 18:17
tensorflow LU ops related
op {
name: "Lu"
input_arg {
name: "input"
type_attr: "T"
}
output_arg {
name: "l"
description: "The lower triangular matrix L."