Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

Graph Data Structure in JavaScript 🚀

A simple implementation of the Graph data structure in JavaScript. This repository demonstrates how to create a graph class with essential methods and explains its functionality with practical examples.


What is a Graph?

A Graph is a collection of nodes (also called vertices) and edges that connect pairs of nodes. Graphs can be either directed (edges have a direction) or undirected (edges do not have a direction). Graphs are used to represent various real-world structures such as networks, maps, and relationships between entities.


Features

  • Add Vertex: Add a new node to the graph.
  • Add Edge: Add a connection between two nodes.
  • Remove Vertex: Remove a node from the graph.
  • Remove Edge: Remove the connection between two nodes.
  • Display: View the graph structure.

Code Implementation

Here’s the JavaScript implementation of the graph:

classGraph{constructor(){this.vertices={};// Stores all vertices (nodes)}// Add a new vertex to the graphaddVertex(vertex){if(!this.vertices[vertex]){this.vertices[vertex]=[];}}// Add an edge between two verticesaddEdge(vertex1,vertex2){if(this.vertices[vertex1]&&this.vertices[vertex2]){this.vertices[vertex1].push(vertex2);this.vertices[vertex2].push(vertex1);// For undirected graph}}// Remove a vertex from the graphremoveVertex(vertex){if(this.vertices[vertex]){deletethis.vertices[vertex];for(letkeyinthis.vertices){constindex=this.vertices[key].indexOf(vertex);if(index!==-1){this.vertices[key].splice(index,1);}}}}// Remove an edge between two verticesremoveEdge(vertex1,vertex2){if(this.vertices[vertex1]&&this.vertices[vertex2]){this.vertices[vertex1]=this.vertices[vertex1].filter(v=>v!==vertex2);this.vertices[vertex2]=this.vertices[vertex2].filter(v=>v!==vertex1);}}// Display the graph structuredisplay(){console.log(this.vertices);}}

Example Usage

// Initialize the graphconstgraph=newGraph();// Add verticesgraph.addVertex("A");graph.addVertex("B");graph.addVertex("C");// Add edgesgraph.addEdge("A","B");graph.addEdge("A","C");// Display the graphgraph.display();// Output: { A: [ 'B', 'C' ], B: [ 'A' ], C: [ 'A' ] }// Remove an edgegraph.removeEdge("A","B");graph.display();// Output: { A: [ 'C' ], B: [], C: [ 'A' ] }// Remove a vertexgraph.removeVertex("C");graph.display();// Output: { A: [], B: [] }

Real-World Applications

  1. Social Networks: Representing users and their connections.
  2. Route Planning: Used in maps for finding the shortest path between locations.
  3. Recommendation Systems: Connecting items based on user preferences or behavior.
  4. Web Crawling: Representing the relationship between web pages and links.

TikTok Tutorial 🎥

Want to see a quick tutorial on how to build this? Check out this TikTok video:


How to Run the Code

  1. Clone the repository:
    git clone https://github.com/fix2015/structure_graph
    cd structure_graph
  2. Open the file index.js in your favorite code editor.
  3. Run the file using Node.js:
    node index.js

Contributing

Contributions are welcome! If you have suggestions or want to add new features, feel free to create a pull request.


License

This project is licensed under the MIT License.


Connect with Me:

About

A **Graph** is a collection of nodes (also called vertices) and edges that connect pairs of nodes. Graphs can be either **directed** (edges have a direction) or **undirected** (edges do not have a direction). Graphs are used to represent various real-world structures such as networks, maps, and relationships between entities.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages