Graph implementation (adjacent linked list C #)

Source: Internet
Author: User
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace adjacencylist {public class adjacencylist <t> {list <vertex <t> items; // public adjacencylist (): This (10) {}// constructor public adjacencylist (INT capacity) // construct based on the specified capacity {items = new list <vertex <t> (capacity );} public void addvertex (T item) // Add a node {If (contains (item) {Throw new argumentexception ("Duplicate nodes are inserted! ");} Items. add (new vertex <t> (item);} public void addedge (T from, t) // Add undirected edge {vertex <t> fromver = find (from); // locate the Start Node if (fromver = NULL) {Throw new argumentexception ("the header node does not exist! ");} Vertex <t> Tover = find (to); // locate the end node if (Tover = NULL) {Throw new argumentexception (" The End Node does not exist! ");} // Adddirectededge (fromver, Tover); adddirectededge (Tover, fromver);} public bool contains (T item) // check whether the graph contains an item {foreach (vertex <t> V in items) {If (v. data. equals (item) {return true;} return false;} private vertex <t> Find (T item) // find the specified item and return {foreach (vertex <t> V in items) {If (v. data. equals (item) {return v ;}} return NULL ;}// Add a directed edge private void adddirectededge (vertex <T> fromver, vertex <t> Tover) {If (fromver. firstedge = NULL) // {fromver. firstedge = new node (Tover);} else {node TMP, node = fromver. firstedge; do {// check whether duplicate edge if (node. adjvex. data. equals (Tover. data) {Throw new argumentexception ("Duplicate edge added! ");} TMP = node; node = node. Next;} while (node! = NULL); TMP. Next = new node (Tover); // Add it to the end of the linked list.} Public override string tostring () // only used for testing {// print each vertex and Its Adjacent point string S = string. empty; foreach (vertex <t> V in items) {S + = v. data. tostring () + ":"; if (v. firstedge! = NULL) {node TMP = V. firstedge; while (TMP! = NULL) {S + = TMP. adjvex. data. tostring (); TMP = TMP. next;} s + = "\ r \ n";} return s;} public void dfstraverse () // deep Priority Search {initvisited (); // set all the visited flags to false DFS (items [0]); // traverse from the first vertex} private void DFS (vertex <t> V) // use recursion for depth-first traversal {v. visited = true; // set the access flag to true console. writeline (v. data + ""); // Access Node = v. firstedge; while (node! = NULL) // all neighboring nodes accessing this vertex {// If the neighboring node is not accessed, recursively access its edge if (! Node. adjvex. visited) {DFS (node. adjvex); // recursion} node = node. next; // access the next node} private void initvisited () // initialize the visited flag {foreach (vertex <t> V in items) {v. visited = false; // set all to false} // search for public void bfstraverse () {initvisited (); // BFS (items [0]); // traverse from the first vertex} private void BFS (vertex <t> V) // search extended queues {// create a queue <vertex <t> queue = new queue <vertex <t> (); console. writeline (v. data + ""); V. visited = true; queue. enqueue (V); While (queue. count> 0) {vertex <t> W = queue. dequeue (); node = W. firstedge; while (node! = NULL) {If (! Node. adjvex. visited) {console. writeline (node. adjvex. data + ""); // Access Node. adjvex. visited = true; // set the access flag queue. enqueue (node. adjvex); // enter} node = node. next; // access the next adjacent node }}// traverse the public void dfstraverse2 () // The depth of the unconnected graph preferentially traverses {initvisitited (); foreach (vertex <t> V in items) {If (! V. visited) {DFS (v) ;}} public void bfstraverse2 () // The breadth of the unconnected graph first traverses {initvisited (); foreach (vertex <t> V in items) {If (! V. visited) {BFS (v) ;}}// nested class indicates the public class node {public vertex <t> adjvex; // The Public node next in the adjacent vertex field; // The next adjacent point pointer field public node (vertex <t> value) {adjvex = value ;}} // The nested class indicates that the header node of the array is saved as public class vertex <tvalue> {public tvalue data; // The data is public node firstedge; // The Public Boolean visited pointer to the adjacent linked list header; // The access flag is used to traverse public vertex (tvalue) {DATA = value ;}}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.