Skip to content

Commit 7fa536b

Browse files
author
Carlos Leonard
committed
update
1 parent 360c9d0 commit 7fa536b

19 files changed

+239
-30
lines changed

12_FarthestNode.cpp

Lines changed: 158 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,182 @@
22
/*
33
have the function FarthestNodes(strArr) read strArr which will be an array of hyphenated letters representing paths between those two nodes. For example: ["a-b","b-c","b-d"] means that there is a path from node a to b (and b to a), b to c, and b to d. Your program should determine the longest path that exists in the graph and return the length of that path. So for the example above, your program should return 2 because of the paths a-b-c and d-b-c. The path a-b-c also means that there is a path c-b-a. No cycles will exist in the graph and every node will be connected to some other node in the graph.
44
*/
5-
65
#include <iostream>
76
#include <string>
87
#include <vector>
9-
#include <stack>
8+
#include <queue>
109
#include <map>
1110
using namespace std;
1211

12+
/*
13+
one approach is to treat each node as a possible starting point from where we can expand and perform BFS
14+
at each step of our BFS we update the cost of each node
15+
this cost determines the length from the current starting node
16+
we repeat this process for all the nodes in our graph and keep track of the highest length
17+
*/
18+
19+
map <char, int> hashTable;
1320

1421
// structure to represent each node in the graph
1522
struct node
1623
{
24+
int cost;
1725
char name;
1826
bool visited;
1927
vector <char> neighbors;
2028
};
2129

30+
// method to create the nodes in our graph
31+
void createGraph(vector <node*>& graph, string values[], int size)
32+
{
33+
int index = 0;
34+
35+
for (int x = 0; x < size; x++)
36+
{
37+
char node1 = values[x][0];
38+
char node2 = values[x][2];
39+
40+
// check if the current node is already in our hash table
41+
// this determines if we need to add it to our graph
42+
// for the first iteration we add the first 2 letters analyzed
43+
if (x == 0)
44+
{
45+
hashTable[node1] = index;
46+
47+
// making the node
48+
graph.push_back(new node);
49+
graph[index]->cost = 0;
50+
graph[index]->visited = false;
51+
graph[index]->name = node1;
52+
index++;
53+
54+
hashTable[node2] = index;
55+
graph.push_back(new node);
56+
graph[index]->cost = 0;
57+
graph[index]->visited = false;
58+
graph[index]->name = node2;
59+
index++;
60+
}
61+
else
62+
{
63+
if (hashTable.count(node1) <= 0)
64+
{
65+
hashTable[node1] = index;
66+
graph.push_back(new node);
67+
graph[index]->cost = 0;
68+
graph[index]->visited = false;
69+
graph[index]->name = node1;
70+
index++;
71+
}
72+
73+
if (hashTable.count(node2) <= 0)
74+
{
75+
hashTable[node2] = index;
76+
graph.push_back(new node);
77+
graph[index]->cost = 0;
78+
graph[index]->visited = false;
79+
graph[index]->name = node2;
80+
index++;
81+
}
82+
}
83+
}
84+
}
85+
86+
// method to create the pair connections
87+
void makeConnections(vector <node*> graph, string values[], int size)
88+
{
89+
for (int x = 0; x < size; x++)
90+
{
91+
char node1 = values[x][0];
92+
char node2 = values[x][2];
93+
94+
// making the bidirectional edge connection between the 2 nodes
95+
graph[hashTable[node1]]->neighbors.push_back(node2);
96+
graph[hashTable[node2]]->neighbors.push_back(node1);
97+
}
98+
}
99+
100+
// method to reset our graph to default values
101+
void resetGraph(vector <node*> graph)
102+
{
103+
for (int x = 0; x < graph.size(); x++)
104+
{
105+
graph[x]->cost = 0;
106+
graph[x]->visited = false;
107+
}
108+
}
109+
110+
// method that return the farthest node from the specified source node
111+
int getFarthest(node* source, vector <node*> graph)
112+
{
113+
// in this method we are applying a simple BFS from the source
114+
queue <node*> list;
115+
list.push(source);
116+
117+
// keep track of farthest length
118+
int length = 0;
119+
120+
while (!list.empty())
121+
{
122+
node* current = list.front();
123+
list.pop();
124+
125+
current->visited = true;
126+
127+
// check the neighbors of current node
128+
for (int x = 0; x < current->neighbors.size(); x++)
129+
{
130+
int index = hashTable[current->neighbors[x]];
131+
132+
// only analyze unvisited neighbors
133+
if (graph[index]->visited == false)
134+
{
135+
// update its cost and compare it to the length
136+
int newLength = current->cost + 1;
137+
graph[index]->cost = newLength;
138+
139+
if (newLength > length)
140+
{
141+
length = newLength;
142+
}
22143

23-
string FarthestNodes(string strArr[], int size)
144+
// adding to our queue
145+
list.push(graph[index]);
146+
}
147+
}
148+
}
149+
150+
return length;
151+
}
152+
153+
int FarthestNodes(string strArr[], int size)
24154
{
155+
int length = 0;
156+
157+
// setting up our graph
158+
vector <node*> graph;
159+
createGraph(graph, strArr, size);
160+
161+
// making the connections
162+
makeConnections(graph, strArr, size);
163+
164+
// in this operation we perform a BFS from every node in our graph
165+
for (int x = 0; x < graph.size(); x++)
166+
{
167+
int currentLength = getFarthest(graph[x], graph);
168+
169+
if (currentLength > length)
170+
{
171+
length = currentLength;
172+
}
173+
174+
// resetting our graph;
175+
resetGraph(graph);
176+
}
25177

178+
hashTable.clear();
179+
180+
return length;
26181
}
27182

28183
int main()

13_VertexCovering.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// For this challenge you will determine whether a given set of vertices can cover all the edges in the graph.
2+
/*
3+
have the function VertexCovering(strArr) take strArr which will be an array of length three. The first part of the array will be a list of vertices in a graph in the form (A,B,C,...), the second part of the array will be the edges connecting the vertices in the form (A-B,C-D,...) where each edge is bidirectional. The last part of the array will be a set of vertices in the form (X,Y,Z,...) and your program will have to determine whether or not the set of vertices given covers every edge in the graph such that every edge is incident to at least one vertex in the set.
4+
5+
For example: if strArr is ["(A,B,C,D)","(A-B,A-D,B-D,A-C)","(A,B)"] then the vertices (A,B) are in fact one of the endpoints of every edge in the graph, so every edge has been accounted for. Therefore your program should return the string yes. But, if for example the last part of the array was (C,B) then these vertices don't cover all the edges because the edge connecting A-D is left out. If this is the case your program should return the edges given in the second part of the array that are left out in the same order they were listed, so for this example your program should return (A-D).
6+
7+
The graph will have at least 2 vertices and all the vertices in the graph will be connected. The third part of the array listing the vertices may be empty, where it would only contain the parenthesis with no values within it: "()"
8+
*/
9+
10+
#include <iostream>
11+
#include <string>
12+
#include <vector>
13+
#include <map>
14+
#include <queue>
15+
using namespace std;
16+
17+
string VertexCovering(string strArr[])
18+
{
19+
20+
}
21+
22+
int main()
23+
{
24+
string A[] = { "(A,B,C,D)", "(A-B,A-D,B-D,A-C)", "(A,B)" };
25+
string B[] = { "(A,B,C,D)", "(A-B,A-D,B-D,A-C)", "(C)" };
26+
string C[] = { "(X,Y,Z,Q)", "(X-Y,Y-Q,Y-Z)", "(Z,Y,Q)" };
27+
28+
cout << VertexCovering(A) << endl; // yes
29+
cout << VertexCovering(A) << endl; // (A-B,A-D,B-D)
30+
cout << VertexCovering(A) << endl; // yes
31+
32+
return 0;
33+
}

Debug/11_WeightedPath.obj

-896 KB
Binary file not shown.
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\vc120.pdb
22
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\vc120.idb
3+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\12_farthestnode.obj
4+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\10_citytraffic.obj
5+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\11_weightedpath.obj
6+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\2_calculator.obj
7+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\4_gasstation.obj
8+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\5_reversepolishnotation.obj
9+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\7_lcs.obj
10+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\8_parallelsums.obj
311
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\code1.obj
412
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\code2.obj
513
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\code3.obj
614
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\code4.obj
715
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\code5.obj
816
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\code6.obj
9-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\yo.obj
10-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\4_gasstation.obj
11-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\5_reversepolishnotation.obj
12-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\2_calculator.obj
13-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\7_lcs.obj
14-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\8_parallelsums.obj
15-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\test.obj
16-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\shortestpath.obj
17-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\10_citytraffic.obj
18-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\menu.obj
1917
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\main.obj
18+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\menu.obj
19+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\shortestpath.obj
20+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\test.obj
2021
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\test2.obj
21-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\11_weightedpath.obj
22-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\debug\fun practice 3.ilk
23-
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\debug\fun practice 3.exe
22+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\yo.obj
23+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\fun practice 3.ilk
24+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\vc140.idb
25+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\fun practice 3.pdb
26+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\vc140.pdb
2427
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\debug\fun practice 3.pdb
25-
c:\users\carlo\documents\github\fun-practice-3\debug\vc120.idb
26-
c:\users\carlo\documents\github\fun-practice-3\debug\fun practice 3.tlog\cl.command.1.tlog
27-
c:\users\carlo\documents\github\fun-practice-3\debug\fun practice 3.tlog\cl.read.1.tlog
28-
c:\users\carlo\documents\github\fun-practice-3\debug\fun practice 3.tlog\cl.write.1.tlog
29-
c:\users\carlo\documents\github\fun-practice-3\debug\fun practice 3.tlog\link.command.1.tlog
30-
c:\users\carlo\documents\github\fun-practice-3\debug\fun practice 3.tlog\link.read.1.tlog
31-
c:\users\carlo\documents\github\fun-practice-3\debug\fun practice 3.tlog\link.write.1.tlog
28+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\fun practice 3.tlog\cl.command.1.tlog
29+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\fun practice 3.tlog\cl.read.1.tlog
30+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\fun practice 3.tlog\cl.write.1.tlog
31+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\fun practice 3.tlog\link.command.1.tlog
32+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\fun practice 3.tlog\link.read.1.tlog
33+
c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\debug\fun practice 3.tlog\link.write.1.tlog

Debug/Fun Practice 3.ilk

-943 KB
Binary file not shown.

Debug/Fun Practice 3.log

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
 11_WeightedPath.cpp
2-
c:\users\carlo\documents\github\fun-practice-3\11_weightedpath.cpp(135): warning C4018: '<': signed/unsigned mismatch
3-
c:\users\carlo\documents\github\fun-practice-3\11_weightedpath.cpp(189): warning C4715: 'WeightedPath': not all control paths return a value
4-
Fun Practice 3.vcxproj -> C:\Users\carlo\Documents\GitHub\Fun-Practice-3\Debug\Fun Practice 3.exe
1+
Build started 12/18/2017 4:12:24 PM.
2+
1>Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice 3\Fun Practice 3\Fun Practice 3.vcxproj" on node 2 (Build target(s)).
3+
1>ClCompile:
4+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _LIB /D _UNICODE /D UNICODE /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt 12_FarthestNode.cpp 13_VertexCovering.cpp
5+
13_VertexCovering.cpp
6+
12_FarthestNode.cpp
7+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\12_farthestnode.cpp(103): warning C4018: '<' : signed/unsigned mismatch
8+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\12_farthestnode.cpp(128): warning C4018: '<' : signed/unsigned mismatch
9+
1>c:\users\gutty333\documents\visual studio 2013\projects\fun practice 3\fun practice 3\12_farthestnode.cpp(165): warning C4018: '<' : signed/unsigned mismatch
10+
Generating Code...
11+
Link:
12+
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice 3\Debug\Fun Practice 3.exe" /INCREMENTAL /NOLOGO kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice 3\Debug\Fun Practice 3.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice 3\Debug\Fun Practice 3.lib" /MACHINE:X86 Debug\12_FarthestNode.obj
13+
Debug\13_VertexCovering.obj
14+
Fun Practice 3.vcxproj -> C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice 3\Debug\Fun Practice 3.exe
15+
1>Done Building Project "C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice 3\Fun Practice 3\Fun Practice 3.vcxproj" (Build target(s)).
16+
17+
Build succeeded.
18+
19+
Time Elapsed 00:00:02.06

Debug/Fun Practice 3.pdb

-2.3 MB
Binary file not shown.
-3.22 KB
Binary file not shown.
1.99 KB
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#TargetFrameworkVersion=v4.0:PlatformToolSet=v140:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=8.1
2-
Debug|Win32|C:\Users\carlo\Documents\GitHub\Fun-Practice-3\|
1+
#TargetFrameworkVersion=v4.0:PlatformToolSet=v120:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit
2+
Debug|Win32|C:\Users\gutty333\Documents\Visual Studio 2013\Projects\Fun Practice 3\|

0 commit comments

Comments
 (0)