Pat:05-1. List components, Go language solutions

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The topic probably means: given a graph with n vertices and e edges, use DFS and BFS to traverse the graph with no direction. The vertex number is assumed to be 0 to N-1, and is always traversed at the lowest numbered vertex and accessed in ascending order when the adjacent node is accessed.

For a given undirected graph with N vertices and E edges, pleaselist all the connected components by both DFS and BFS. Assume thatall the vertices is numbered from 0 to N-1. While searching,assume this we always start from the vertex with the Smallestindex, and visit their adjacent vertices in ASC Ending order of theirindices.

Input Specification:

Each input file contains the one test case. For each case, the firstline gives the integers N (0

Output Specification:

For each test case, the print in each line a connected component inthe format "{v1 v2 ... vk}". First print the result obtained Bydfs, then by BFS.

Sample Input:

8 60 70 12 04 12 43 5

Sample Output:

{0 1 4 2 7} {3 5} {6} {0 1 2 7 4} {3 5} {6}
001 PackageMain
002
003 Import (
004 "FMT"
005 "Container/heap"
006 "Container/list"
007 )
008 var visited = Make([]BOOL, Ten)//topic gives no more than 10 vertex count, at least 1
009 var G = Make([]Intheap,Ten)
010
011 func Main(){
012 varNUMOFV,Numofeint
013 //Accept vertex count and number of sides
014 _, Err:=FMT.Scanf("%d%d\n",&NUMOFV,&Numofe)
015 ifErr !=Nil {
016 FMT.Println("Error",Err)
017 }
018 forI :=0; I<</SPAN>Numofe;I++{//Load data into vertex list
019 var v1,v2int
020 _, Err =FMT.Scanf("%d%d\n",&v1,&v2)
021 if Err != Nil {
022 FMT.Println("Error",Err)
023 }
024 Heap.Push(&G[v1],v2)
025 Heap.Push(&G[v2],v1)
026 }
027 //************************************
028 //Start traversal
029 //*************************************
030 forI:= 0; I<</SPAN>NUMOFV;I++ {
031 if !visited[I]{
032 FMT.Print("{ ")
033 DFS(I);
034 FMT.Print("}\n")
035 }
036 }
037 //Reset visited array
038 forI :=Range visited {
039 visited[I]=false
040 }
041 forI :=0; I<</SPAN>NUMOFV;I++ {
042 if !visited[I]{
043 FMT.Print("{ ")
044 BFS(I)
045 FMT.Print("}\n")
046 }
047 }
048 }
049 //Depth Priority search (Depth first Search, DFS):
050 //access to the next visible element that is not accessed,
051 //If all visible elements have been accessed, the previous element is returned
052 func DFS(vint) (){
053 visited[v]=true
054 FMT.Printf("%d",v)
055 for_,Neibor:= Range G[v]{//Traverse adjacent nodes
056 if !visited[Neibor]{
057 DFS(Neibor)
058 }
059 }
060 }
061 //Breadth Priority search (breadth First search, BFS):
062 //similar to the idea of sequence traversal, using the queue to operate, first put 1 into the queue, when the team, the 1 of all the neighboring points into the queue
063 func BFS(vint) (){
064 varls =List.New()
065 visited[v]=true
066 FMT.Printf("%d",v)
067 ls.Pushback(v)
068 forls.Len()!= 0 {
069 Ele := ls.Front()
070 ls.Remove(Ele)
071 Thev :=Ele.Value. (int)//thev.value is an interface type, so it is necessary to assert
072
073 for _,Neibor:= Range G[Thev]{//Traverse adjacent nodes
074 if !visited[Neibor] {
075 visited[Neibor]= true
076 FMT.Printf("%d",Neibor)
077 ls.Pushback(Neibor)
078 }
079 }
080 }
081 }
082
083 //heap operation, Minimum heap
084 type Intheap []int
085
086 func (hIntheap)Len()int{ returnLen(h)}
087 func (hIntheap) Less(I,Jint) BOOL { return h[I]<</SPAN>h[J] }
088 func (hIntheap)Swap(I,Jint){ h[I], h[J]=h[J], h[I]}
089
090 func (h*Intheap)Push(xInterface{}){
091 //Push Andpop use pointer receivers because they modify the slice ' slength,
092 //Not justits contents.
093 *h =Append(*h,x. (int))
094 }
095
096 func (h*Intheap)Pop() Interface{} {
097 Old:= *h
098 N:= Len( Old)
099 x:= Old[N-1]
- *h = Old[0: N-1]
101 returnx
102 }

copyright notice: This article for Bo Master original article, without Bo Master permission not reproduced.

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.