Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<queue>
- #include<list>
- #include<stack>
- using namespace std;
- typedef struct node
- {
- int index;
- int value;
- int bestValue;
- bool operator < (const node &lhs)
- {
- return 0;
- }
- }node;
- node createNode(int index, int value)
- {
- node temp;
- temp.index = index;
- temp.value = value;
- temp.bestValue = temp.value;
- return temp;
- }
- node dfsCount = createNode(0,0);
- class graph
- {
- list<node> *totalVertex;
- int vertex;
- public:
- graph(int vertex)
- {
- this->vertex = vertex;
- totalVertex = new list<node>[vertex];
- }
- void addEdge(node position, node value)
- {
- totalVertex[position.index].push_back(value);
- }
- int BFS(node startpos)
- {
- bool hasChild =false;
- list<node> queue;
- list<int>queuesum;
- int sum=0;
- int pushValue;
- int smallestValue = 20000;
- queue.push_back(startpos);
- queuesum.push_back(startpos.value);
- list<node>::iterator i;
- while (!queue.empty())
- {
- hasChild = false;
- startpos = queue.front();
- sum = queuesum.front();
- queue.pop_front();
- queuesum.pop_front();
- for (i = totalVertex[startpos.index].begin(); i != totalVertex[startpos.index].end(); ++i)
- {
- hasChild = true;
- pushValue = i->value+ sum;
- queuesum.push_back(pushValue);
- queue.push_back(*i);
- }
- if (!hasChild)
- {
- if (sum < smallestValue)
- {
- smallestValue = sum;
- }
- }
- }
- return smallestValue;
- }
- int dijkstraBFS(node startpos)
- {
- bool hasChild = false;
- priority_queue<node> queue;
- priority_queue<int>queuesum;
- int sum = 0;
- int pushValue;
- int smallestValue = 20000;
- queue.push(startpos);
- queuesum.push(startpos.value);
- list<node>::iterator i;
- while (!queue.empty())
- {
- hasChild = false;
- startpos = queue.top();
- sum = queuesum.top();
- queue.pop();
- queuesum.pop();
- for (i = totalVertex[startpos.index].begin(); i != totalVertex[startpos.index].end(); ++i)
- {
- hasChild = true;
- pushValue = i->value + sum;
- queuesum.push(pushValue);
- queue.push(*i);
- }
- if (!hasChild)
- {
- if (sum < smallestValue)
- {
- smallestValue = sum;
- }
- }
- }
- return smallestValue;
- }
- } ;
- int main()
- {
- int finalValue=0;
- graph triangle(120);
- //row 0
- node node0 = createNode(0, 75);
- //row 1
- node node1 = createNode(1, 95);
- node node2 = createNode(2, 64);
- //row 3
- node node3 = createNode(3, 17);
- node node4 = createNode(4, 47);
- node node5 = createNode(5, 82);
- //row 4
- node node6 = createNode(6, 18);
- node node7 = createNode(7, 35);
- node node8 = createNode(8, 87);
- node node9 = createNode(9, 10);
- //row 5
- node node10 = createNode(10, 20);
- node node11 = createNode(11, 4);
- node node12 = createNode(12, 82);
- node node13 = createNode(13, 47);
- node node14 = createNode(14, 65);
- //row 6
- node node15 = createNode(15, 19);
- node node16 = createNode(16, 1);
- node node17 = createNode(17, 23);
- node node18 = createNode(18, 75);
- node node19 = createNode(19, 3);
- node node20 = createNode(20, 34);
- //row 7
- node node21 = createNode(21, 88);
- node node22 = createNode(22, 2);
- node node23 = createNode(23, 77);
- node node24 = createNode(24, 73);
- node node25 = createNode(25, 7);
- node node26 = createNode(26, 63);
- node node27 = createNode(27, 67);
- //row 8
- node node28 = createNode(28, 99);
- node node29 = createNode(29, 65);
- node node30 = createNode(30, 4);
- node node31 = createNode(31, 28);
- node node32 = createNode(32, 6);
- node node33 = createNode(33, 16);
- node node34 = createNode(34, 70);
- node node35 = createNode(35, 92);
- //row 9
- node node36 = createNode(36, 41);
- node node37 = createNode(37, 41);
- node node38 = createNode(38, 26);
- node node39 = createNode(39, 56);
- node node40 = createNode(40, 83);
- node node41 = createNode(41, 40);
- node node42 = createNode(42, 80);
- node node43 = createNode(43, 70);
- node node44 = createNode(44, 33);
- //row 10
- node node45 = createNode(45, 41);
- node node46 = createNode(46, 48);
- node node47 = createNode(47, 72);
- node node48 = createNode(48, 33);
- node node49 = createNode(49, 47);
- node node50 = createNode(50, 32);
- node node51 = createNode(51, 37);
- node node52 = createNode(52, 16);
- node node53 = createNode(53, 94);
- node node54 = createNode(54, 29);
- //row 11
- node node55 = createNode(55, 53);
- node node56 = createNode(56, 71);
- node node57 = createNode(57, 44);
- node node58 = createNode(58, 65);
- node node59 = createNode(59, 25);
- node node60 = createNode(60, 43);
- node node61 = createNode(61, 91);
- node node62 = createNode(62, 52);
- node node63 = createNode(63, 97);
- node node64 = createNode(64, 51);
- node node65 = createNode(65, 14);
- //row 12
- node node66 = createNode(66, 70);
- node node67 = createNode(67, 11);
- node node68 = createNode(68, 33);
- node node69 = createNode(69, 28);
- node node70 = createNode(70, 77);
- node node71 = createNode(71, 73);
- node node72 = createNode(72, 17);
- node node73 = createNode(73, 78);
- node node74 = createNode(74, 39);
- node node75 = createNode(75, 68);
- node node76 = createNode(76, 17);
- node node77 = createNode(77, 57);
- //row 13
- node node78 = createNode(78, 91);
- node node79 = createNode(79, 71);
- node node80 = createNode(80, 52);
- node node81 = createNode(81, 38);
- node node82 = createNode(82, 17);
- node node83 = createNode(83, 14);
- node node84 = createNode(84, 91);
- node node85 = createNode(85, 43);
- node node86 = createNode(86, 58);
- node node87 = createNode(87, 50);
- node node88 = createNode(88, 27);
- node node89 = createNode(89, 29);
- node node90 = createNode(90, 48);
- //row 14
- node node91 = createNode(91, 63);
- node node92 = createNode(92, 66);
- node node93 = createNode(93, 4);
- node node94 = createNode(94, 68);
- node node95 = createNode(95, 89);
- node node96 = createNode(96, 53);
- node node97 = createNode(97, 67);
- node node98 = createNode(98, 30);
- node node99 = createNode(99, 73);
- node node100 = createNode(100, 16);
- node node101 = createNode(101, 69);
- node node102 = createNode(102, 87);
- node node103 = createNode(103, 40);
- node node104 = createNode(104, 31);
- //row 15
- node node105 = createNode(105, 4);
- node node106 = createNode(106, 62);
- node node107 = createNode(107, 98);
- node node108 = createNode(108, 27);
- node node109 = createNode(109, 23);
- node node110 = createNode(110, 9);
- node node111 = createNode(111, 70);
- node node112 = createNode(112, 98);
- node node113 = createNode(113, 73);
- node node114 = createNode(114, 93);
- node node115 = createNode(115, 38);
- node node116 = createNode(116, 53);
- node node117 = createNode(117, 60);
- node node118 = createNode(118, 4);
- node node119 = createNode(119, 23);
- triangle.addEdge(node0, node1);
- triangle.addEdge(node0, node2);
- triangle.addEdge(node1, node3);
- triangle.addEdge(node1, node4);
- triangle.addEdge(node2, node4);
- triangle.addEdge(node2, node5);
- triangle.addEdge(node3, node6);
- triangle.addEdge(node3, node7);
- triangle.addEdge(node4, node7);
- triangle.addEdge(node4, node8);
- triangle.addEdge(node5, node8);
- triangle.addEdge(node5, node9);
- triangle.addEdge(node6, node10);
- triangle.addEdge(node6, node11);
- triangle.addEdge(node7, node11);
- triangle.addEdge(node7, node12);
- triangle.addEdge(node8, node12);
- triangle.addEdge(node8, node13);
- triangle.addEdge(node9, node13);
- triangle.addEdge(node9, node14);
- triangle.addEdge(node10, node15);
- triangle.addEdge(node10, node16);
- triangle.addEdge(node11, node16);
- triangle.addEdge(node11, node17);
- triangle.addEdge(node12, node17);
- triangle.addEdge(node12, node18);
- triangle.addEdge(node13, node18);
- triangle.addEdge(node13, node19);
- triangle.addEdge(node14, node19);
- triangle.addEdge(node14, node20);
- triangle.addEdge(node15, node21);
- triangle.addEdge(node15, node22);
- triangle.addEdge(node16, node22);
- triangle.addEdge(node16, node23);
- triangle.addEdge(node17, node23);
- triangle.addEdge(node17, node24);
- triangle.addEdge(node18, node24);
- triangle.addEdge(node18, node25);
- triangle.addEdge(node19, node25);
- triangle.addEdge(node19, node26);
- triangle.addEdge(node20, node26);
- triangle.addEdge(node20, node27);
- triangle.addEdge(node21, node28);
- triangle.addEdge(node21, node29);
- triangle.addEdge(node22, node29);
- triangle.addEdge(node22, node30);
- triangle.addEdge(node23, node30);
- triangle.addEdge(node23, node31);
- triangle.addEdge(node24, node31);
- triangle.addEdge(node24, node32);
- triangle.addEdge(node25, node32);
- triangle.addEdge(node25, node33);
- triangle.addEdge(node26, node33);
- triangle.addEdge(node26, node34);
- triangle.addEdge(node27, node34);
- triangle.addEdge(node27, node35);
- triangle.addEdge(node28, node36);
- triangle.addEdge(node28, node37);
- triangle.addEdge(node29, node37);
- triangle.addEdge(node29, node38);
- triangle.addEdge(node30, node38);
- triangle.addEdge(node30, node39);
- triangle.addEdge(node31, node39);
- triangle.addEdge(node31, node40);
- triangle.addEdge(node32, node40);
- triangle.addEdge(node32, node41);
- triangle.addEdge(node33, node41);
- triangle.addEdge(node33, node42);
- triangle.addEdge(node34, node42);
- triangle.addEdge(node34, node43);
- triangle.addEdge(node35, node43);
- triangle.addEdge(node35, node44);
- triangle.addEdge(node36, node45);
- triangle.addEdge(node36, node46);
- triangle.addEdge(node37, node46);
- triangle.addEdge(node37, node47);
- triangle.addEdge(node38, node47);
- triangle.addEdge(node38, node48);
- triangle.addEdge(node39, node48);
- triangle.addEdge(node39, node49);
- triangle.addEdge(node40, node49);
- triangle.addEdge(node40, node50);
- triangle.addEdge(node41, node50);
- triangle.addEdge(node41, node51);
- triangle.addEdge(node42, node51);
- triangle.addEdge(node42, node52);
- triangle.addEdge(node43, node52);
- triangle.addEdge(node43, node53);
- triangle.addEdge(node44, node53);
- triangle.addEdge(node44, node54);
- triangle.addEdge(node45, node55);
- triangle.addEdge(node45, node56);
- triangle.addEdge(node46, node56);
- triangle.addEdge(node46, node57);
- triangle.addEdge(node47, node57);
- triangle.addEdge(node47, node58);
- triangle.addEdge(node48, node58);
- triangle.addEdge(node48, node59);
- triangle.addEdge(node49, node59);
- triangle.addEdge(node49, node60);
- triangle.addEdge(node50, node60);
- triangle.addEdge(node50, node61);
- triangle.addEdge(node51, node61);
- triangle.addEdge(node51, node62);
- triangle.addEdge(node52, node62);
- triangle.addEdge(node52, node63);
- triangle.addEdge(node53, node63);
- triangle.addEdge(node53, node64);
- triangle.addEdge(node54, node64);
- triangle.addEdge(node54, node65);
- triangle.addEdge(node55, node66);
- triangle.addEdge(node55, node67);
- triangle.addEdge(node56, node67);
- triangle.addEdge(node56, node68);
- triangle.addEdge(node57, node68);
- triangle.addEdge(node57, node69);
- triangle.addEdge(node58, node69);
- triangle.addEdge(node58, node70);
- triangle.addEdge(node59, node70);
- triangle.addEdge(node59, node71);
- triangle.addEdge(node60, node71);
- triangle.addEdge(node60, node72);
- triangle.addEdge(node61, node72);
- triangle.addEdge(node61, node73);
- triangle.addEdge(node62, node73);
- triangle.addEdge(node62, node74);
- triangle.addEdge(node63, node74);
- triangle.addEdge(node63, node75);
- triangle.addEdge(node64, node75);
- triangle.addEdge(node64, node76);
- triangle.addEdge(node65, node76);
- triangle.addEdge(node65, node77);
- triangle.addEdge(node66, node78);
- triangle.addEdge(node66, node79);
- triangle.addEdge(node67, node79);
- triangle.addEdge(node67, node80);
- triangle.addEdge(node68, node80);
- triangle.addEdge(node68, node81);
- triangle.addEdge(node69, node81);
- triangle.addEdge(node69, node82);
- triangle.addEdge(node70, node82);
- triangle.addEdge(node70, node83);
- triangle.addEdge(node71, node83);
- triangle.addEdge(node71, node84);
- triangle.addEdge(node72, node84);
- triangle.addEdge(node72, node85);
- triangle.addEdge(node73, node85);
- triangle.addEdge(node73, node86);
- triangle.addEdge(node74, node86);
- triangle.addEdge(node74, node87);
- triangle.addEdge(node75, node87);
- triangle.addEdge(node75, node88);
- triangle.addEdge(node76, node88);
- triangle.addEdge(node76, node89);
- triangle.addEdge(node77, node89);
- triangle.addEdge(node77, node90);
- triangle.addEdge(node78, node91);
- triangle.addEdge(node78, node92);
- triangle.addEdge(node79, node92);
- triangle.addEdge(node79, node93);
- triangle.addEdge(node80, node93);
- triangle.addEdge(node80, node94);
- triangle.addEdge(node81, node94);
- triangle.addEdge(node81, node95);
- triangle.addEdge(node82, node95);
- triangle.addEdge(node82, node96);
- triangle.addEdge(node83, node96);
- triangle.addEdge(node83, node97);
- triangle.addEdge(node84, node97);
- triangle.addEdge(node84, node98);
- triangle.addEdge(node85, node98);
- triangle.addEdge(node85, node99);
- triangle.addEdge(node86, node99);
- triangle.addEdge(node86, node100);
- triangle.addEdge(node87, node100);
- triangle.addEdge(node87, node101);
- triangle.addEdge(node88, node101);
- triangle.addEdge(node88, node102);
- triangle.addEdge(node89, node102);
- triangle.addEdge(node89, node103);
- triangle.addEdge(node90, node103);
- triangle.addEdge(node90, node104);
- triangle.addEdge(node91, node105);
- triangle.addEdge(node91, node106);
- triangle.addEdge(node92, node106);
- triangle.addEdge(node92, node107);
- triangle.addEdge(node93, node107);
- triangle.addEdge(node93, node108);
- triangle.addEdge(node94, node108);
- triangle.addEdge(node94, node109);
- triangle.addEdge(node95, node109);
- triangle.addEdge(node95, node110);
- triangle.addEdge(node96, node110);
- triangle.addEdge(node96, node111);
- triangle.addEdge(node97, node111);
- triangle.addEdge(node97, node112);
- triangle.addEdge(node98, node112);
- triangle.addEdge(node98, node113);
- triangle.addEdge(node99, node113);
- triangle.addEdge(node99, node114);
- triangle.addEdge(node100, node114);
- triangle.addEdge(node100, node115);
- triangle.addEdge(node101, node115);
- triangle.addEdge(node101, node116);
- triangle.addEdge(node102, node116);
- triangle.addEdge(node102, node117);
- triangle.addEdge(node103, node117);
- triangle.addEdge(node103, node118);
- triangle.addEdge(node104, node118);
- triangle.addEdge(node104, node119);
- //finalValue = triangle.DFS(node0);
- //cout << "Final DFS Total: " << finalValue << endl;
- cout << endl;
- finalValue = triangle.BFS(node0);
- cout << "Final BFS total: " << finalValue;
- cout << endl;
- cout << endl;
- system("Pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement