Coder Social home page Coder Social logo

lab11-summer2023's Introduction

Graph Implementation

In this lab you will add additional methods to the UnweightedGraph class from lecture. This graph is directed and unweighted. In the IDE of your choice clone the repository to download the following starter files: Edge.java, Graph.java, and UnweightedGraph.java.

In order to create an UnweightedGraph object, write a method that reads in Integer graph data from a file and sets this UnweightedGraph instance according to the data in the file. The method signature must be

public static UnweightedGraph<Integer> graphFromFile(String fileName) throws FileNotFoundException

Your method will accept a file name including the file extension, for example, txt, as a String parameter to create and return an UnweightedGraph object. If the file is unable to be opened, the method should throw a FileNotFoundException The first line in the file contains a number that indicates the number of vertices (n). The vertices are labeled as 0, 1, …, n−1. Each subsequent line, with the format originVertex destVertex1 destVertex 2 …, describes edges (originVertex, destVertex 1), (originVertex, destVertex 2), and so on.

Here are examples of two files for their corresponding graphs: Image of sample file contents and corresponding graphs

To the UnweightedGraph class, implement the following methods:

public boolean isComplete() // returns true if this graph is complete

  • use the number of vertices and the calculation to determine

public boolean areAdjacent(V v1, V v2) // returns true if v1 is adjacent to v2

public boolean isConnected()           // returns true if the graph is connected

  • Call the printEdges() to display all edges, and then call dfs() to obtain an instance tree of UnweightedGraph.SearchTree. If tree.getNumberOfVerticesFound() is the same as the number of vertices in the graph, the graph is connected.

public List<Integer> getSortestPath(int u, int v)

  • The method returns a List that contains all the vertices in the shortest path from u to v in order.
  • Using the breadth-first search approach, you can obtain the shortest path from u to v.
  • If there isn’t a path from u to v, the method returns null.

public boolean hasCycle()

  • The method returns true if there is a cycle in this instance of UnweightedGraph.

lab11-summer2023's People

Contributors

debmduke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.