Download free for 30 days
Sign in
Upload
Language (EN)
Support
Business
Mobile
Social Media
Marketing
Technology
Art & Photos
Career
Design
Education
Presentations & Public Speaking
Government & Nonprofit
Healthcare
Internet
Law
Leadership & Management
Automotive
Engineering
Software
Recruiting & HR
Retail
Sales
Services
Science
Small Business & Entrepreneurship
Food
Environment
Economy & Finance
Data & Analytics
Investor Relations
Sports
Spiritual
News & Politics
Travel
Self Improvement
Real Estate
Entertainment & Humor
Health & Medicine
Devices & Hardware
Lifestyle
Change Language
Language
English
Español
Português
Français
Deutsche
Cancel
Save
EN
Uploaded by
Nguyễn Công Hoàng
554 views
Kruskal algorithm
Source code Kruskal algorithm by Tran Dan Thu
Education
◦
Entertainment & Humor
◦
Business
◦
Read more
1
Save
Share
Embed
Embed presentation
Download
Downloaded 26 times
1
/ 5
2
/ 5
3
/ 5
4
/ 5
5
/ 5
More Related Content
PDF
Node.JS
by
eibaan
PDF
Фатальный недостаток Node.js
by
Oleksii Okhrymenko
PPTX
Rubyslava2102
by
Marius Sajgalik
DOCX
Linklist through struct
by
IBRAR GUL
PDF
Modern Mobile Web Apps
by
dynamis
TXT
Practica 4 errores
by
UVM
TXT
vecotores programacion
by
Alex Flores Avalos
PDF
contoh Program C++ tentang fungsi for
by
M Fahmi Ansori
Node.JS
by
eibaan
Фатальный недостаток Node.js
by
Oleksii Okhrymenko
Rubyslava2102
by
Marius Sajgalik
Linklist through struct
by
IBRAR GUL
Modern Mobile Web Apps
by
dynamis
Practica 4 errores
by
UVM
vecotores programacion
by
Alex Flores Avalos
contoh Program C++ tentang fungsi for
by
M Fahmi Ansori
What's hot
DOCX
Dsa 1
by
Harisrasheed48
DOC
Correcion
by
diegorap
DOCX
Info clasa
by
Ilinca Ostroschi
DOCX
Teknik simulasi
by
qoriaini
PDF
modern javascript, unobtrusive javascript, jquery
by
Adam Zygadlewicz
PDF
Virapix-ClassDiagram
by
Mandar Kashelikar
PDF
Gaztea Tech 2015: 4. GT Drawbot Control
by
Svet Ivantchev
PDF
Rambler.iOS #8: Чистые unit-тесты
by
RAMBLER&Co
PDF
Week 7 unit3 (chapter 10-11)
by
aj.mapling
TXT
Python codigo graficas
by
Brayan Kalaka
PDF
Progressive Mobile Web Apps
by
dynamis
DOCX
Algoritmo secuencial
by
Daneziita Laulate Flores
PDF
Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
by
Tomohiro Kumagai
DOCX
Novatadas en java
by
wmca28
PPTX
JavaScript Assíncrono
by
Natã Barbosa
TXT
Zadatak
by
Zeljko Lukic
PDF
Programación funcional en Haskell
by
Roberto Bonvallet
Dsa 1
by
Harisrasheed48
Correcion
by
diegorap
Info clasa
by
Ilinca Ostroschi
Teknik simulasi
by
qoriaini
modern javascript, unobtrusive javascript, jquery
by
Adam Zygadlewicz
Virapix-ClassDiagram
by
Mandar Kashelikar
Gaztea Tech 2015: 4. GT Drawbot Control
by
Svet Ivantchev
Rambler.iOS #8: Чистые unit-тесты
by
RAMBLER&Co
Week 7 unit3 (chapter 10-11)
by
aj.mapling
Python codigo graficas
by
Brayan Kalaka
Progressive Mobile Web Apps
by
dynamis
Algoritmo secuencial
by
Daneziita Laulate Flores
Swift イニシャライザー復習会 #love_swift #akibaswift #21cafe
by
Tomohiro Kumagai
Novatadas en java
by
wmca28
JavaScript Assíncrono
by
Natã Barbosa
Zadatak
by
Zeljko Lukic
Programación funcional en Haskell
by
Roberto Bonvallet
More from Nguyễn Công Hoàng
PPS
Dieubatngochoem
by
Nguyễn Công Hoàng
PDF
Lap trinh huong doi tuong voi java tran dinh que
by
Nguyễn Công Hoàng
PPT
Timkiem&sapxep
by
Nguyễn Công Hoàng
PDF
Giao trinh ky thuat lap trinh 2
by
Nguyễn Công Hoàng
PDF
Phong cách lập trình - Đặng Bình Phương
by
Nguyễn Công Hoàng
PPT
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
by
Nguyễn Công Hoàng
PPT
Giáo trình Phân tích và thiết kế giải thuật - CHAP 6
by
Nguyễn Công Hoàng
PPT
Giáo trình Phân tích và thiết kế giải thuật - CHAP 5
by
Nguyễn Công Hoàng
PPT
Giáo trình Phân tích và thiết kế giải thuật - CHAP 4
by
Nguyễn Công Hoàng
PPT
Giáo trình Phân tích và thiết kế giải thuật - CHAP 3
by
Nguyễn Công Hoàng
PPT
Giáo trình Phân tích và thiết kế giải thuật - CHAP 2
by
Nguyễn Công Hoàng
PPT
Giáo trình Phân tích và thiết kế giải thuật - CHAP 1
by
Nguyễn Công Hoàng
PPT
Giáo trình Phân tích và thiết kế giải thuật - CHAP 7
by
Nguyễn Công Hoàng
PPS
Lời khuyên
by
Nguyễn Công Hoàng
Dieubatngochoem
by
Nguyễn Công Hoàng
Lap trinh huong doi tuong voi java tran dinh que
by
Nguyễn Công Hoàng
Timkiem&sapxep
by
Nguyễn Công Hoàng
Giao trinh ky thuat lap trinh 2
by
Nguyễn Công Hoàng
Phong cách lập trình - Đặng Bình Phương
by
Nguyễn Công Hoàng
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
by
Nguyễn Công Hoàng
Giáo trình Phân tích và thiết kế giải thuật - CHAP 6
by
Nguyễn Công Hoàng
Giáo trình Phân tích và thiết kế giải thuật - CHAP 5
by
Nguyễn Công Hoàng
Giáo trình Phân tích và thiết kế giải thuật - CHAP 4
by
Nguyễn Công Hoàng
Giáo trình Phân tích và thiết kế giải thuật - CHAP 3
by
Nguyễn Công Hoàng
Giáo trình Phân tích và thiết kế giải thuật - CHAP 2
by
Nguyễn Công Hoàng
Giáo trình Phân tích và thiết kế giải thuật - CHAP 1
by
Nguyễn Công Hoàng
Giáo trình Phân tích và thiết kế giải thuật - CHAP 7
by
Nguyễn Công Hoàng
Lời khuyên
by
Nguyễn Công Hoàng
Kruskal algorithm
1.
// File: Kruskal.h------------------------------------------------------------- #ifndef
_MY_KGRAPH_H #define _MY_KGRAPH_H #include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; class Edge { int v1, v2; float w; public: Edge(int v1=0, int v2=0, float w=0); int getV1(); int getV2(); float getW(); ostream& Show(ostream& outDev) const; friend bool operator < (const Edge& e1, const Edge& e2); friend bool operator == (const Edge& e1, const Edge& e2); friend istream& operator >> (istream&, Edge&); friend ostream& operator << (ostream&, const Edge&); }; class KruskalAlgo { vector<int> Label; vector<Edge> Edges; vector<Edge> T; int nVer; void UpdateLabels(int v1, int v2); void InitAlg(); public: void LoadEdges(istream& inDev) ; bool LoadEdges(char* fName) ; int RunAlg(); void ShowT(ostream& outDev); }; #endif // end of File: Kruskal.h-------------------------------------------------------
2.
// File Kruskal.cpp----------------------------------- #include
"Kruskal.h" // Methods and operators for Edge class Edge::Edge(int v1, int v2, float w){ this->w=w; this->v1=v1; this->v2=v2; } int Edge::getV1() { return v1; } int Edge::getV2() { return v2; } float Edge::getW() { return w; } ostream& Edge::Show(ostream& outDev) const { outDev << v1 << "----"<< v2 << " ; w = " << w; return outDev; } bool operator < (const Edge& e1, const Edge& e2){ return e1.w < e2.w; } bool operator == (const Edge& e1, const Edge& e2){ return e1.w == e2.w; } istream& operator >> (istream& inDev, Edge& e){ inDev >> e.v1 >> e.v2 >> e.w; return inDev; } ostream& operator << (ostream& outDev, const Edge& e){ return e.Show(outDev); }
3.
// Methods and
operators for KruskalAlgo class void KruskalAlgo::LoadEdges(istream& inDev) { int nE; Edge e; inDev >> nVer >> nE; for (int i=0; i<nE; i++){ inDev >> e; Edges.push_back(e); } } bool KruskalAlgo::LoadEdges(char* fName) { ifstream inFile(fName); if (inFile){ LoadEdges(inFile); return true; } return false; } void KruskalAlgo::UpdateLabels(int v1, int v2) { if (v1<0 || v1 >= nVer || v2<0 || v2>=nVer){ return; } int minLabel = Label[v1], maxLabel = Label[v2]; if (minLabel > Label[v2]){ minLabel = Label[v2]; maxLabel = Label[v1]; } for (int i=0; i<nVer; i++) if (Label[i] == maxLabel){ Label[i] = minLabel; } } /* LƯU Ý: Ghi nhận đoạn mã sai sau đây đế rút kinh nghiệm: if (Label[v1] < Label[v2]){ for (int i=0; i<nVer; i++) if (Label[i] == Label[v2]) Label[i] = Label[v1]; } else{ for (int i=0; i<nVer; i++) if (Label[i] == Label[v1]) Label[i] = Label[v2]; }*/
4.
void KruskalAlgo::InitAlg(){
Label.resize(nVer); for (int i=0; i<Label.size(); i++){ Label[i]=i; } sort(Edges.begin(), Edges.end()); } int KruskalAlgo::RunAlg(){ // Thuaät toaùn chính InitAlg(); // Böôùc 1 int nE = Edges.size(), nT = 0; for (int i=0; i<nE; i++) { // Böôùc 2 Edge e=Edges[i]; int v1=e.getV1(), v2=e.getV2(); if (Label[v1] != Label[v2]){ T.push_back(e); UpdateLabels(v1, v2); } if ( (nT=T.size()) == nVer-1) break; } return nT==nVer-1; } void KruskalAlgo::ShowT(ostream& outDev) { int nT = T.size(); float w=0; for (int i=0; i<nT; i++){ outDev << T[i] << endl; w += T[i].getW(); } outDev << "Total weight = " << w << endl; } // end of File Kruskal.cpp---------------------------
5.
// File MainPrg.cpp--------------------------------------- #include
"Kruskal.h" #include <conio.h> void main() // Lấy dữ liệu từ file { KruskalAlgo g; if (g.LoadEdges("gData.txt")){ g.RunAlg(); g.ShowT(cout); } else cout << "File not found!" << endl; getch(); } /* void main() // Lấy dữ liệu từ thiết bị nhập chuẩn (bàn phím console hay tập tin) { KruskalAlgo g; g.LoadEdges(cin); g.RunAlg(); g.ShowT(cout); getch(); } */ // end of File MainPrg.cpp-------------------------------------------
Download