Study notes of achieving a senior software engineer

图算法精要
本文深入探讨了图算法的核心概念及实现细节,包括迪杰斯特拉算法、弗洛伊德算法、福特-福克森算法、贝尔曼-福德算法等。通过具体示例展示了这些算法的应用场景及其复杂度。

All notes are from internet and they are only for my personally quick revision. 


Necessary algorithms for Google Interview





1.binary tree 










2. graph study notes


3.Dijkstra Algorithm

/*
 Petar 'PetarV' Velickovic
 Algorithm: Dijkstra's Algorithm
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 100001
#define INF 987654321
using namespace std;
typedef long long lld;

int n;

struct Node
{
    int dist;
    vector<int> adj;
    vector<int> weight;
};
Node graf[MAX_N];
bool mark[MAX_N];

struct pq_entry
{
    int node, dist;
    bool operator <(const pq_entry &a) const
    {
        if (dist != a.dist) return (dist > a.dist); // get the smallest value,  due to greater than
        return (node > a.node);
    }
};

//Dijkstrin algoritam za nalazenje duzina najkracih puteva iz jednog izvora u grafu
//Slozenost: O((V+E)log V)

inline void Dijkstra(int source)
{
    priority_queue<pq_entry> pq;
    pq_entry P;
    for (int i=0;i<n;i++)
    {
        if (i == source)
        {
            graf[i].dist = 0;
            P.node = i;
            P.dist = 0;
            pq.push(P);
        }
        else graf[i].dist = INF;
    } //initialise the date, 
    while (!pq.empty())
    {
        pq_entry curr = pq.top();// get the least number
        pq.pop();
        int nod = curr.node;
        int dis = curr.dist;
        for (int i=0;i<graf[nod].adj.size();i++)
        {
            if (!mark[graf[nod].adj[i]])// iterate all adjacent notes
            {
                int nextNode = graf[nod].adj[i];
                if (dis + graf[nod].weight[i] < graf[nextNode].dist)
                {
                    graf[nextNode].dist = dis + graf[nod].weight[i];
                    P.node = nextNode;
                    P.dist = graf[nextNode].dist;
                    pq.push(P);
                }
            }
        }
        mark[nod] = true;
    }
}

int main()
{
    n = 4;
    
    graf[0].adj.push_back(1);
    graf[0].weight.push_back(5);
    graf[1].adj.push_back(0);
    graf[1].weight.push_back(5);
    
    graf[1].adj.push_back(2);
    graf[1].weight.push_back(5);
    graf[2].adj.push_back(1);
    graf[2].weight.push_back(5);
    
    graf[2].adj.push_back(3);
    graf[2].weight.push_back(5);
    graf[3].adj.push_back(2);
    graf[3].weight.push_back(5);
    
    graf[3].adj.push_back(1);
    graf[3].weight.push_back(6);
    graf[1].adj.push_back(3);
    graf[1].weight.push_back(6);
    
    Dijkstra(0);
    
    printf("%d\n",graf[3].dist);
    return 0;
}

4.Floyd-Warshall Algorithm

/*
 Petar 'PetarV' Velickovic 
 Algorithm: Floyd-Warshall Algorithm
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 300
#define INF 987654321
using namespace std;
typedef long long lld;

int n;

int dist[MAX_N][MAX_N];
int flojd[MAX_N][MAX_N];

//Floyd-Warshallov algoritam za trazenje duzina najkracih puteva svih parova cvorova u grafu
//Slozenost: O(V^3)

inline void FloydWarshall()
{
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=n;j++)
        {
            flojd[i][j] = dist[i][j];
        }
        flojd[i][i] = 0;
    }
    for (int k=1;k<=n;k++)
    {
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=n;j++)
            {
                if (flojd[i][k] + flojd[k][j] < flojd[i][j])
                {
                    flojd[i][j] = flojd[i][k] + flojd[k][j];
                }
            }
        }
    }
}

int main()
{
    n = 3;
    dist[1][1] = 0, dist[1][2] = 3, dist[1][3] = INF;
    dist[2][1] = INF, dist[2][2] = 0, dist[2][3] = 4;
    dist[3][1] = INF, dist[3][2] = 1, dist[3][3] = 0;
    FloydWarshall();
    printf("%d\n",flojd[1][3]);
    return 0;
}

5.Ford-Fulkerson Algorithm

经典讲解:https://www.youtube.com/watch?v=GiN3jRdgxU4

typical concepts of the flow network:




/*
 Petar 'PetarV' Velickovic
 Algorithm: Ford-Fulkerson Algorithm
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 500
#define INF 987654321
using namespace std;
typedef long long lld;

struct Node
{
    vector<int> adj;
};
Node graf[MAX_N];
bool mark[MAX_N];
int cap[MAX_N][MAX_N];
int parent[MAX_N];

int v, e;
int s, t;

//Ford-Fulkersonov algoritam za nalazenje maksimalnog protoka izmedju dva cvora u grafu
//Moze se koristiti i za nalazenje maksimalnog matchinga
//Slozenost: O(E * maxFlow)

inline int DFS()
{
    int ret = 0;
    for (int i=1;i<=v;i++) parent[i] = 0;
    stack<int> dfs_stek;
    stack<int> minCapacity;
    parent[s] = -1;
    dfs_stek.push(s);
    minCapacity.push(INF);
    while (!dfs_stek.empty())
    {
        int xt = dfs_stek.top();
        int mt = minCapacity.top();
        dfs_stek.pop();
        minCapacity.pop();
        if (xt == t)
        {
            ret = mt;
            break;
        }
        for (int i=0;i<graf[xt].adj.size();i++)
        {
            int xt1 = graf[xt].adj[i];
            if (cap[xt][xt1] > 0 && parent[xt1] == 0)
            {
                dfs_stek.push(xt1);
                minCapacity.push(min(mt,cap[xt][xt1]));
                parent[xt1] = xt;
            }
        }
    }
    if (ret > 0)
    {
        int currNode = t;
        while (currNode != s)
        {
            cap[parent[currNode]][currNode] -= ret;
            cap[currNode][parent[currNode]] += ret;
            currNode = parent[currNode];
        }
    }
    return ret;
}

inline int FordFulkerson()
{
    int flow = 0;
    while (true)
    {
        int currFlow = DFS();
        if (currFlow == 0) break;
        else flow += currFlow;
    }
    return flow;
}

int main()
{
    v = 4, e = 5;
    s = 1, t = 4;
    
    graf[1].adj.push_back(2);
    graf[2].adj.push_back(1);
    cap[1][2] = 40;
    
    graf[1].adj.push_back(4);
    graf[4].adj.push_back(1);
    cap[1][4] = 20;
    
    graf[2].adj.push_back(4);
    graf[4].adj.push_back(2);
    cap[2][4] = 20;
    
    graf[2].adj.push_back(3);
    graf[3].adj.push_back(2);
    cap[2][3] = 30;
    
    graf[3].adj.push_back(4);
    graf[4].adj.push_back(3);
    cap[3][4] = 10;
    
    printf("%d\n",FordFulkerson());
    
    return 0;
}

6.Bellman-Ford Algorithm

/*
 Petar 'PetarV' Velickovic
 Algorithm: Bellman-Ford Algorithm
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 5001
#define MAX_E 25000001
#define INF 987654321
using namespace std;
typedef long long lld;

int v, e;

int dist[MAX_N];
struct Edge
{
    int x, y, weight;
};
Edge E[MAX_N];

//Bellman-Ford algoritam za trazenje najkracih puteva iz odredjenog cvora u grafu (graf moze imati i negativne ivice)
//Slozenost: O(V*E)

inline int BellmanFord(int source)
{
    for (int i=0;i<v;i++)
    {
        if (i == source) dist[i]=0;
        else dist[i] = INF;
    }
    bool done = false;
    for (int i=0;!done&&i<v;i++)
    {
        done = true;
        for (int j=0;j<e;j++)
        {
            int so = E[j].x;
            int de = E[j].y;
			cout<<"dist[so "<<so<<"]="<<dist[so]<<endl;
			cout<<"dist[de "<<de<<"]="<<dist[de]<<" weight="<<E[j].weight<<endl;
            if (dist[so] + E[j].weight < dist[de])
            {
                dist[de] = dist[so] + E[j].weight;
                done=false;
				cout<<"<<<<<<<<<<<<<<"<<endl;
            }
			cout<<"-------------dist[de"<<de<<"]="<<dist[de]<<endl;
			cout<<"-------------dist[so"<<so<<"]="<<dist[so]<<endl;
			cout<<"j="<<j<<endl;
        }
		cout<<"---done="<<done<<endl;
    }
    if (!done) return -1; //negative edge cycle detected
    return 0;
}

int main()
{
    v = 4, e = 8;
    
    E[0].x = 0, E[0].y = 1, E[0].weight = 5;
    E[1].x = 1, E[1].y = 0, E[1].weight = 5;
    
    E[2].x = 1, E[2].y = 2, E[2].weight = 5;
    E[3].x = 2, E[3].y = 1, E[3].weight = 5;
    
    E[4].x = 2, E[4].y = 3, E[4].weight = 5;
    E[5].x = 3, E[5].y = 2, E[5].weight = 5;
    
    E[6].x = 3, E[6].y = 1, E[6].weight = 6;
    E[7].x = 1, E[7].y = 3, E[7].weight = 6;
    
    BellmanFord(0);
    printf("%d\n",dist[3]);
    return 0;
}

**项目名称:** 基于Vue.js与Spring Cloud架构的博客系统设计与开发——微服务分布式应用实践 **项目概述:** 本项目为计算机科学与技术专业本科毕业设计成果,旨在设计并实现一个采用前后端分离架构的现代化博客平台。系统前端基于Vue.js框架构建,提供响应式用户界面;后端采用Spring Cloud微服务架构,通过服务拆分、注册发现、配置中心及网关路由等技术,构建高可用、易扩展的分布式应用体系。项目重点探讨微服务模式下的系统设计、服务治理、数据一致性及部署运维等关键问题,体现了分布式系统在Web应用中的实践价值。 **技术架构:** 1. **前端技术栈:** Vue.js 2.x、Vue Router、Vuex、Element UI、Axios 2. **后端技术栈:** Spring Boot 2.x、Spring Cloud (Eureka/Nacos、Feign/OpenFeign、Ribbon、Hystrix、Zuul/Gateway、Config) 3. **数据存储:** MySQL 8.0(主数据存储)、Redis(缓存与会话管理) 4. **服务通信:** RESTful API、消息队列(可选RabbitMQ/Kafka) 5. **部署与运维:** Docker容器化、Jenkins持续集成、Nginx负载均衡 **核心功能模块:** - 用户管理:注册登录、权限控制、个人中心 - 文章管理:富文本编辑、分类标签、发布审核、评论互动 - 内容展示:首页推荐、分类检索、全文搜索、热门排行 - 系统管理:后台仪表盘、用户与内容监控、日志审计 - 微服务治理:服务健康检测、动态配置更新、熔断降级策略 **设计特点:** 1. **架构解耦:** 前后端完全分离,通过API网关统一接入,支持独立开发与部署。 2. **服务拆分:** 按业务域划分为用户服务、文章服务、评论服务、文件服务等独立微服务。 3. **高可用设计:** 采用服务注册发现机制,配合负载均衡与熔断器,提升系统容错能力。 4. **可扩展性:** 模块化设计支持横向扩展,配置中心实现运行时动态调整。 **项目成果:** 完成了一个具备完整博客功能、具备微服务典型特征的分布式系统原型,通过容器化部署验证了多服务协同运行的可行性,为云原生应用开发提供了实践参考。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
Once upon a time, there was a student named Sarah. Sarah was a dedicated student who always worked hard to excel in her studies. She was always the first one to arrive in class and the last one to leave. Sarah was passionate about learning and was always eager to expand her knowledge in various subjects. Despite her passion for learning, Sarah faced a lot of challenges in her academic journey. She struggled with mathematics and often found herself spending hours studying and practicing. However, Sarah never gave up and continued to work hard to overcome her weaknesses. One day, her hard work paid off when she received an A in her math test. She was overjoyed and felt a sense of accomplishment that she had never felt before. From that day on, Sarah was motivated to work even harder. As Sarah's academic journey progressed, she faced many other obstacles such as time management, stress, and peer pressure. However, she remained determined and focused on her goals. She joined study groups and sought help from her teachers whenever she needed it. Despite the challenges, Sarah graduated with flying colors and was accepted into her dream university. She majored in biology and went on to pursue a career in the medical field. Sarah's dedication and hard work had paid off, and she was now on her way to achieving her dreams. In conclusion, Sarah's story is a testament to the fact that with hard work, determination, and perseverance, anything is possible. She faced many obstacles in her academic journey, but she never gave up. She continued to work hard and eventually achieved her goals. Sarah is an inspiration to all students who are struggling with their studies.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值