Algorithm-direction graph and accessibility

Source: Internet
Author: User

Figure is connected by vertices and edges, if the edge is no direction is the previous article in the non-directed graph, about the non-directed graph can refer to my previous article, if the edge is a direction, it is called a directed graph. From the vertex a→b, we can understand that a to B can be reached, the directed graph and the non-directed graph to save each edge through the adjacency table, because the edges are directional, so in the process of adding edges only need to add an edge. For accessibility of a node array, the approach is to use the same code as the previous depth-first search, to determine the accessibility of each vertex in the array by recursively using the token-bit bool marker bit. To test, select the following map:

the basic of a graph

Through the picture we can find that there are 13 nodes in the graph, 22 edges, vertex 0 points out the node has 1, 5, the pointed node has 2, 6, we first implement all vertex points of the node, and then can be reversed to determine the point of all nodes of the node:

Total number of digraph:nsobject//vertices of the @interface @property  (assign,nonatomic) nsinteger  vertexs;//sides @property (Assign , nonatomic) Nsinteger  edges;//The edge of the connection point @property (strong,nonatomic)  Nsmutablearray *adjdatasource;-  ( Instancetype) Initwithvertex: (Nsinteger) vertexs;//add a forward edge startvertex→endvertex-(void) Addedges: (NSInteger) Startvertex  Endvertex: (Nsinteger) endvertex;-(Digraph *) reverse;//the inverse diagram of the graph @end

Implementation code:

@implementation digraph-(Instancetype) Initwithvertex: (nsinteger) vertexs{self=[super init];        if (self) {self.vertexs=vertexs; for (Nsinteger i=0; i<vertexs; i++) {Nsmutablearray *neighbourvertex=[[nsmutablearray Alloc]initWithCapaci            TY:1]; [Self.adjdatasource addobject:neighbourvertex];//Create adjacency table, initialize all linked lists to null}} return self; http://www.cnblogs.com/xiaofeixiang-(void) Addedges: (Nsinteger) Startvertex Endvertex: (nsinteger) endVertex{// Add Endvertex to Startvertex's list [Self.adjdatasource[startvertex] Insertobject:[nsnumber Numberwithinteger:endvertex]    ATINDEX:0]; self.edges=self.edges+1;}    -(Digraph *) reverse{Digraph *digraph=[[digraph Alloc]initwithvertex:self.vertexs];        for (Nsinteger i=0; i<self.vertexs; i++) {Nsmutablearray *temparr=self.adjdatasource[i];        for (Nsinteger j=0; J<[temparr Count]; j + +) {[digraph addedges:[temparr[j] integervalue] endvertex:i]; }} return digRaph;} #pragma mark Getter and setter-(Nsmutablearray *) adjdatasource{if (!_adjdatasource) {_ADJDATASOURCE=[[NSMUTABL    Earray Alloc]initwithcapacity:1]; } return _adjdatasource;} @end

test code:

        Digraph *graph=[[digraph alloc]initwithvertex:13];        [Graph Addedges:4 Endvertex:2];        [Graph Addedges:2 Endvertex:3];        [Graph Addedges:3 Endvertex:2];        [Graph Addedges:6 endvertex:0];        [Graph addedges:0 endvertex:1];        [Graph Addedges:2 endvertex:0];        [Graph Addedges:11 Endvertex:12];        [Graph Addedges:12 Endvertex:9];        [Graph Addedges:9 endvertex:10];        [Graph Addedges:9 endvertex:11];        [Graph Addedges:8 Endvertex:9];        [Graph Addedges:10 Endvertex:12];        [Graph Addedges:11 Endvertex:4];        [Graph Addedges:4 Endvertex:3];        [Graph Addedges:3 Endvertex:5];        [Graph Addedges:7 Endvertex:8];        [Graph Addedges:8 Endvertex:7];        [Graph Addedges:5 Endvertex:4];        [Graph addedges:0 Endvertex:5];        [Graph Addedges:6 Endvertex:4];        [Graph Addedges:6 Endvertex:9];        [Graph Addedges:7 Endvertex:6];       for (Nsinteger i=0; I<[graph.adjdatasource count]; i++) {     NSLog (@ "node%ld indicates → node:%@", I,[graph.adjdatasource[i] componentsjoinedbystring:@ "--"]);        } NSLog (@ "Technology Exchange Group:%@", @ "228407086"); NSLog (@ "Original address: Http://www.cnblogs.com/xiaofeixiang");

Test results:

Now you can determine the vertex node, the implementation file has a reverse method to reverse the graph, to find the vertex node:

        Digraph  *digraph=[graph reverse];        for (Nsinteger i=0; I<[digraph.adjdatasource count]; i++) {            NSLog (@) in%ld?? Node:%@ ", I,[digraph.adjdatasource[i] componentsjoinedbystring:@"--"]);        }        NSLog (@ "Technology Exchange Group:%@", @ "228407086");        NSLog (@ "Original address: Http://www.cnblogs.com/xiaofeixiang");

Test results:

Accessibility

Accessibility judgments and previous depth-first searches are basically unchanged, first look at the methods that need to be implemented:

@interface directeddfs:nsobject//Tag Array @property  (strong,nonatomic)  Nsmutablearray  *marked;// Find all vertices in arr-(instancetype) Initdirecteddfswithvertex: (Digraph *) Graph  Vertexarr: (Nsarray *) arr;// Find all vertices available from vertex in graph-(void) Directeddfs: (Digraph *) Graph  vertex: (Nsinteger) Vertex;//vertex is up to-(Boolean) Ismarked: (Nsinteger) vertex; @end

Implementation code:

@implementation Directeddfs#pragma Mark Getter and setter-(Nsmutablearray *) marked{if (!_marked) {_marked=[[n    Smutablearray Alloc]initwithcapacity:1]; } return _marked;}    -(Instancetype) Initdirecteddfswithvertex: (Digraph *) Graph Vertexarr: (Nsarray *) arr{self=[super init];        if (self) {for (Nsinteger i=0; i<graph.vertexs;i++) {[self.marked addobject:[nsnull null]]; }//Traverse the vertex for (Nsinteger j=0; J<[arr Count]; j + +) {if (![            Self ismarked:[arr[j] integervalue]) {[self directeddfs:graph vertex:[arr[j] integervalue]]; }}} return self;} Blog Park-flyelephant:http://www.cnblogs.com/xiaofeixiang/-(void) Directeddfs: (Digraph *) graph vertex: (Nsinteger)    vertex{Self.marked[vertex]=[nsnumber Numberwithbool:true]; for (Nsinteger i=0; I<[graph.adjdatasource[vertex] count]; i++) {Nsinteger Temp=[[graph.adjdatasource[vertex] o        BJECTATINDEX:I] IntegerValue]; if (![Self ismarked:temp])        {[self directeddfs:graph vertex:temp]; }}}-(Boolean) ismarked: (Nsinteger) vertex{return self.marked[vertex]==[nsnull Null]?false:[self.marked[vertex] bool Value];} @end

Test code:

        Nsarray  *sources=[nsarray arraywithobjects:@ "2", nil];        Directeddfs  *directeddfs=[[directeddfs alloc]initdirecteddfswithvertex:graph vertexarr:sources];        Nsmutablearray  *reachablearr=[[nsmutablearray alloc]initwithcapacity:1];        for (Nsinteger i=0; i<graph.vertexs; i++) {                        if (directeddfs.marked[i]&&directeddfs.marked[i]!=[nsnull NULL]) {                [Reachablearr addobject:[nsnumber numberwithinteger:i];            }        }        NSLog (@ "Up to Node:%@", [Reachablearr componentsjoinedbystring:@ "--"]);        NSLog (@ "Technology Exchange Group:%@", @ "228407086");        NSLog (@ "blog Park-flyelephant:http://www.cnblogs.com/xiaofeixiang");

Test results:

Algorithm-direction graph and accessibility

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.