...
/Solution Review: Count the Number of Edges in an Undirected Graph
Solution Review: Count the Number of Edges in an Undirected Graph
This review provides a detailed analysis of the different ways to solve the "Count the Number of Edges in Graph" challenge.
We'll cover the following...
Solution: Iteration
...Press + to interact
main.cs
LinkedList.cs
Graph.cs
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace chapter_5{public class LinkedList{public class Node{internal int data; //Data to store (could be int,string,object etc)internal Node nextElement; //Pointer to next elementpublic Node(){//Constructor to initialize nextElement of newlyCreated NodenextElement = null;}};Node head;public LinkedList(){head = null;}public Node GetHead(){return head;}bool IsEmpty(){if (head == null) //Check whether the head points to nullreturn true;elsereturn false;}public bool PrintList(){if (IsEmpty()){Console.Write("List is Empty!");return false;}Node temp = head;Console.Write("List : ");while (temp != null){Console.Write(temp.data + "->");temp = temp.nextElement;}Console.WriteLine("null ");return true;}public void InsertAtHead(int value){Node newNode = new Node();newNode.data = value;newNode.nextElement = head; //Linking newNode to head's nextNodehead = newNode;}public string Elements(){ // this function will return all values of linked Liststring elementsList = "";Node start = head;Node check = head;elementsList += start.data.ToString();elementsList += "->";start = start.nextElement;while (start != null && start.data != check.data){elementsList += start.data.ToString();elementsList += "->";start = start.nextElement;}if (start == null)return elementsList + "null";elementsList += check.data.ToString();return elementsList;}public void InsertAtTail(int value){if (IsEmpty()){ // inserting first element in listInsertAtHead(value); // head will point to first element of the list}else{Node newNode = new Node(); // creating new nodeNode last = head; // last pointing at the head of the listwhile (last.nextElement != null){ // traversing through the listlast = last.nextElement;}newNode.data = value;Console.Write(value + " Inserted! ");newNode.nextElement = null; // point last's nextElement to nullptrlast.nextElement = newNode; // adding the newNode at the end of the list}}// function to check if element exists in the listpublic bool Search(int value){Node temp = head; // pointing temp to the headwhile (temp != null){if (temp.data == value){ // if passed value matches with temp's datareturn true;}temp = temp.nextElement; // pointig to temp's nextElement}return false; // if not found}public bool Delete(int value){bool deleted = false; //returns true if the node is deleted, false otherwiseif (IsEmpty()){ //check if the list is emptyConsole.WriteLine("List is Empty");return deleted; //deleted will be false}//if list is not empty, start traversing it from the headNode currentNode = head; //currentNode to traverse the listNode previousNode = null; //previousNode starts from nullif (currentNode.data == value){ // deleting value of headdeleted = DeleteAtHead();Console.WriteLine(value + " deleted!");deleted = true; // true because value found and deletedreturn deleted; //returns true if the node is deleted}previousNode = currentNode;currentNode = currentNode.nextElement; // pointing current to current's nextElement//Traversing/Searching for Node to Deletewhile (currentNode != null){//Node to delete is foundif (value == currentNode.data){// pointing previousNode's nextElement to currentNode's nextElementpreviousNode.nextElement = currentNode.nextElement;// delete currentNode;currentNode = previousNode.nextElement;deleted = true;break;}previousNode = currentNode;currentNode = currentNode.nextElement; // pointing current to current's nextElement}//deleted is true only when value is found and deletedif (deleted == false){Console.WriteLine(value + " does not exist!");}else{Console.WriteLine(value + " deleted!");}return deleted;} //end of delete()bool DeleteAtHead(){if (IsEmpty()){ // check if list is emptyConsole.WriteLine("List is Empty");return false;}head = head.nextElement; //nextNode points to head's nextElementreturn true;}public int Length(){Node current = head; // Start from the first elementint count = 0; // in start count is 0while (current != null){ // Traverse the list and count the number of nodescount++; // increment everytime as element is foundcurrent = current.nextElement; // pointing to current's nextElement}return count;}public string Reverse(){Node previous = null; //To keep track of the previous element, will be used in swapping linksNode current = head; //firstElementNode next = null;//While Traversing the list, swap linkswhile (current != null){next = current.nextElement;current.nextElement = previous;previous = current;current = next;}head = previous; // pointing head to start of the listreturn Elements(); // calling elements to return a string of values in list}public bool DetectLoop(){Node slow = head, fast = head; //starting from head of the listwhile ((slow != null) && (fast != null) && (fast.nextElement != null)) //checking if all elements exist{slow = slow.nextElement;fast = fast.nextElement.nextElement;/* If slow and fast meet at some point then thereis a loop */if (slow == fast){// Return 1 to indicate that loop is found */return true;}}// Return 0 to indeciate that ther is no loop*/return false;}public void InsertLoop(){Node temp = head;// traversing to get to last element of the listwhile (temp.nextElement != null){temp = temp.nextElement;}temp.nextElement = head; // pointing last element to head of the list}public int FindMid(){//list is emptyif (IsEmpty())return 0;//currentNode starts at the headNode currentNode = head;if (currentNode.nextElement == null){//Only 1 element exist in array so return its value.return currentNode.data;}Node midNode = currentNode; //midNode starts at headcurrentNode = currentNode.nextElement.nextElement; //currentNode moves two steps forward//Move midNode (Slower) one step at a time//Move currentNode (Faster) two steps at a time//When currentNode reaches at end, midNode will be at the middle of Listwhile (currentNode != null){ // traversing from head to endmidNode = midNode.nextElement;currentNode = currentNode.nextElement; // pointing to current's nextif (currentNode != null)currentNode = currentNode.nextElement; // pointing to current's next}if (midNode != null) // pointing at middle of the listreturn midNode.data;return 0;}public string RemoveDuplicates(){Node start, startNext = null;start = head;/* Pick elements one by one */while ((start != null) && (start.nextElement != null)){startNext = start;/* Compare the picked element with restof the elements */while (startNext.nextElement != null){/* If duplicate then delete it */if (start.data == startNext.nextElement.data){// skipping elements from the list to be deletedstartNext.nextElement = startNext.nextElement.nextElement;}elsestartNext = startNext.nextElement; // pointing to next of startNext}start = start.nextElement;}return Elements();}public string Union(LinkedList list1, LinkedList list2){//Return other List if one of them is emptyif (list1.IsEmpty())return list2.Elements();else if (list2.IsEmpty())return list1.Elements();Node start = list1.head; // starting from head of list 1//Traverse first list till the last elementwhile (start.nextElement != null){start = start.nextElement;}//Link last element of first list to the first element of second liststart.nextElement = list2.head; // appendinf list2 with list 1return list1.RemoveDuplicates(); // removing duplicates from list and return list}//To Find nth node from end of listpublic int FindNth(int n){if (IsEmpty()) // if list is empty return -1return -1;int length = 0;Node currentNode = head; // pointing to head of the list// finding the length of the listwhile (currentNode != null){currentNode = currentNode.nextElement;length++;}//Find the Node which is at (len - n) position from startcurrentNode = head;int position = length - n;if (position < 0 || position > length) // check if out of the range of the listreturn -1;int count = 0;// traversing till the nth Element of the listwhile (count != position){ // finding the position of the elementcurrentNode = currentNode.nextElement;count++;}if (currentNode != null) // if node existsreturn currentNode.data;return -1;}}}
There is ...