{"id":25490189,"url":"https://github.com/fix2015/structure_graph","last_synced_at":"2025-11-08T12:30:36.505Z","repository":{"id":273081180,"uuid":"918652921","full_name":"fix2015/structure_graph","owner":"fix2015","description":"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.  ","archived":false,"fork":false,"pushed_at":"2025-01-18T14:05:24.000Z","size":7,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-18T15:19:16.863Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fix2015.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-18T14:03:13.000Z","updated_at":"2025-01-18T14:41:22.000Z","dependencies_parsed_at":"2025-01-18T15:19:18.604Z","dependency_job_id":"ca552a17-4491-441e-96bb-1639bc14ab1d","html_url":"https://github.com/fix2015/structure_graph","commit_stats":null,"previous_names":["fix2015/structure_graph"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_graph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_graph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_graph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fix2015%2Fstructure_graph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fix2015","download_url":"https://codeload.github.com/fix2015/structure_graph/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239552865,"owners_count":19658003,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-18T21:27:28.210Z","updated_at":"2025-11-08T12:30:36.440Z","avatar_url":"https://github.com/fix2015.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Graph Data Structure in JavaScript 🚀  \n\nA 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.  \n\n---\n\n## What is a Graph?  \nA **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.  \n\n---\n\n## Features  \n- **Add Vertex**: Add a new node to the graph.  \n- **Add Edge**: Add a connection between two nodes.  \n- **Remove Vertex**: Remove a node from the graph.  \n- **Remove Edge**: Remove the connection between two nodes.  \n- **Display**: View the graph structure.  \n\n---\n\n## Code Implementation  \n\nHere’s the JavaScript implementation of the graph:  \n\n```javascript\nclass Graph {\n    constructor() {\n        this.vertices = {}; // Stores all vertices (nodes)\n    }\n\n    // Add a new vertex to the graph\n    addVertex(vertex) {\n        if (!this.vertices[vertex]) {\n            this.vertices[vertex] = [];\n        }\n    }\n\n    // Add an edge between two vertices\n    addEdge(vertex1, vertex2) {\n        if (this.vertices[vertex1] \u0026\u0026 this.vertices[vertex2]) {\n            this.vertices[vertex1].push(vertex2);\n            this.vertices[vertex2].push(vertex1); // For undirected graph\n        }\n    }\n\n    // Remove a vertex from the graph\n    removeVertex(vertex) {\n        if (this.vertices[vertex]) {\n            delete this.vertices[vertex];\n            for (let key in this.vertices) {\n                const index = this.vertices[key].indexOf(vertex);\n                if (index !== -1) {\n                    this.vertices[key].splice(index, 1);\n                }\n            }\n        }\n    }\n\n    // Remove an edge between two vertices\n    removeEdge(vertex1, vertex2) {\n        if (this.vertices[vertex1] \u0026\u0026 this.vertices[vertex2]) {\n            this.vertices[vertex1] = this.vertices[vertex1].filter(v =\u003e v !== vertex2);\n            this.vertices[vertex2] = this.vertices[vertex2].filter(v =\u003e v !== vertex1);\n        }\n    }\n\n    // Display the graph structure\n    display() {\n        console.log(this.vertices);\n    }\n}\n```\n\n---\n\n## Example Usage  \n\n```javascript\n// Initialize the graph\nconst graph = new Graph();\n\n// Add vertices\ngraph.addVertex(\"A\");\ngraph.addVertex(\"B\");\ngraph.addVertex(\"C\");\n\n// Add edges\ngraph.addEdge(\"A\", \"B\");\ngraph.addEdge(\"A\", \"C\");\n\n// Display the graph\ngraph.display(); // Output: { A: [ 'B', 'C' ], B: [ 'A' ], C: [ 'A' ] }\n\n// Remove an edge\ngraph.removeEdge(\"A\", \"B\");\ngraph.display(); // Output: { A: [ 'C' ], B: [], C: [ 'A' ] }\n\n// Remove a vertex\ngraph.removeVertex(\"C\");\ngraph.display(); // Output: { A: [], B: [] }\n```\n\n---\n\n## Real-World Applications  \n1. **Social Networks**: Representing users and their connections.  \n2. **Route Planning**: Used in maps for finding the shortest path between locations.  \n3. **Recommendation Systems**: Connecting items based on user preferences or behavior.  \n4. **Web Crawling**: Representing the relationship between web pages and links.  \n\n---\n\n## TikTok Tutorial 🎥  \nWant to see a quick tutorial on how to build this? Check out this TikTok video:  \n[]()  \n\n---\n\n## How to Run the Code  \n1. Clone the repository:  \n   ```bash\n   git clone https://github.com/fix2015/structure_graph\n   cd structure_graph\n   ```\n2. Open the file `index.js` in your favorite code editor.  \n3. Run the file using Node.js:  \n   ```bash\n   node index.js\n   ```\n\n---\n\n## Contributing  \nContributions are welcome! If you have suggestions or want to add new features, feel free to create a pull request.  \n\n---\n\n## License  \nThis project is licensed under the MIT License.  \n\n---\n\n## Connect with Me:\n- [LinkedIn - Vitalii Semianchuk](https://www.linkedin.com/in/vitalii-semianchuk-9812a786/)\n- [Telegram - @jsmentorfree](https://t.me/jsmentorfree) - We do a lot of free teaching on this channel! Join us to learn and grow in web development.\n- [Tiktok - @jsmentoring](https://www.tiktok.com/@jsmentoring) Everyday new videos\n- [Youtube - @jsmentor-uk](https://www.youtube.com/@jsmentor-uk) Mentor live streams\n- [Dev.to - fix2015](https://dev.to/fix2015) Javascript featured, live, experience but about Graph\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffix2015%2Fstructure_graph","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffix2015%2Fstructure_graph","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffix2015%2Fstructure_graph/lists"}