University of South Florida JavaScript Graph Creation Project Hi, I need JavaScript and corresponding HTML code to do the following:
Given the Graph shown (ignore pointers this is an undirected graph in that you can traverse from node to node in any direction)
Allow the user to select 2 nodes. Then calculate and display the cost of each path between the 2 nodes.
F to C
F, D, C: 2
F, D, E, C: 4
F, D, B, A, C: 5
F, D, G, B, A, C: 7
In your code, give one application of where a Graph is used by nearly everyone on nearly a daily basis (with the cost function). This is not a trick question.
Construct a graph that represents the assignment
Traverse the graph
Find the optimal path
Additional Info:
var Node = function(_name) {
this.name = _name;
this.edges = [];
return this;
}
In the sample implementation Edges also keep track of their nodes and their cost
var Edge = function(_cost) {
this.from = null;
this.to = null;
this.cost = null;
return this;
}
Note that this information is redundant, the Graph can contain a collection of Edges or a collection of Nodes both will work.
A very commonly used representation of a Graph (basically because the code for this is readily available with a Google Search is this representation;
function graph() {
this.edges = {};
this.paths = [];
}
graph.prototype.add_node = function (label) {
this.edges[label] = {};
};
graph.prototype.add_edge = function (from, to, cost) {
// undirected graph
this.edges[from][to] = cost;
this.edges[to][from] = cost;
};
In this representation the property edges is defined as an object in the constructor of the graph; this.edges = {}.
In the add_node the index of the edges property is set defined as an object in the add_node function; this.edges[label] = {}.
Finally when adding an edge the code sets up a 2D array that allows accessing the cost value through the from and to variables (which must correspond to the string labels of the nodes); this.edges[from][to] = cost.
A recursive function is then employed to create all the paths through the graph. The exit clause of the recursive function is when the graph loops back on itself; if (from == to) display_path(paths, cost). paths is simply an array of all the paths calculated.
Finally the simplest representation of a Graph is simply as a collection of Nodes. You still need edges, but they are not object members of the Graph.
var Graph = function() {
this.nodes = [];
}
var Node = function(Name) {
this.name = Name;
this.edges = [];
return this;
}
var Edge = function(From, To, Cost) {
this.from = From;
this.to = To;
this.cost = Cost;
return this;
}
Additional Information
You will work with graphs a lot in your career, they are everywhere and as an IT professional you are expected to know what they are and how they can be represented. So here is your chance to learn.
There are multiple ways to represent a Graph you should at least know some of them. The discussion at Khan Academy is excellent for this https://www.khanacademy.org/computing/computer-science/algorithms/graph-representation/a/representing-graphs
Another representation is an adjacency matrix, though I personally believe an adjacency list is a simpler and easier to understand representation of a graph discussion of both of these are here http://www.geeksforgeeks.org/graph-and-its-representations/ . The examples are implemented in C, though the translation to javascript is not terrible (if you need help with any of the steps use the discussion board).
Once you have selected a representation of the Graph the next step is the implementation of a traversal algorithm.
Why Choose Us
Top quality papers
We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.
Professional academic writers
We have hired a team of professional writers experienced in academic and business writing. Most of them are native speakers and PhD holders able to take care of any assignment you need help with.
Free revisions
If you feel that we missed something, send the order for a free revision. You will have 10 days to send the order for revision after you receive the final paper. You can either do it on your own after signing in to your personal account or by contacting our support.
On-time delivery
All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.
Original & confidential
We use several checkers to make sure that all papers you receive are plagiarism-free. Our editors carefully go through all in-text citations. We also promise full confidentiality in all our services.
24/7 Customer Support
Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.
Try it now!
How it works?
Follow these simple steps to get your paper done
Place your order
Fill in the order form and provide all details of your assignment.
Proceed with the payment
Choose the payment system that suits you most.
Receive the final file
Once your paper is ready, we will email it to you.
Our Services
No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.
Essays
You are welcome to choose your academic level and the type of your paper. Our academic experts will gladly help you with essays, case studies, research papers and other assignments.
Admissions
Admission help & business writing
You can be positive that we will be here 24/7 to help you get accepted to the Master’s program at the TOP-universities or help you get a well-paid position.
Reviews
Editing your paper
Our academic writers and editors will help you submit a well-structured and organized paper just on time. We will ensure that your final paper is of the highest quality and absolutely free of mistakes.
Reviews
Revising your paper
Our academic writers and editors will help you with unlimited number of revisions in case you need any customization of your academic papers