意外的数字常量图

时间:2014-11-01 18:36:51

标签: r graph

要求是使用R base函数来构建这样的函数:

功能 - is_isomorphic

输入 - g1,图形对象; g2,图形对象。

输出 - 如果g1和g2是同构的,则为true,否则为false。

描述 - 检查图形对象是否是同构的,意味着所有顶点,边和权重都相同。顶点的比较应基于名称而不是索引,只有在未定义顶点标签时才应使用索引。

我的代码如下:

isomorphic <- function(g1,g2){
  ##first calculate number of vertices, if not equal, return false
  if(length(g1) != length(g2)){
    return FALSE
  ##then compare each vertix and its related edges and weights
  } else {
    for(i in 1:length(g1)){
      for(j in 1:length(g2)){
        if(names(g1[i]) == names(g2[j]){
          flag = 1
          if(g1[[i]]$edges != g2[[j]]$edges){
              return FALSE            
          } else {
            if(g1[[i]]$weights != g2[[j]]$weights){
              return FALSE
            } else {
              return TRUE
            }
          }
        }   
      }

      if(flag != 1){
        return FALSE
      }
    }    
  }  
}

wrong message: 
isomorphic <- function(g1,g2){
+   ##first calculate number of vertices, if not equal, return false
+   if(length(g1) != length(g2)){
+     return FALSE
Error: unexpected numeric constant in:
"  if(length(g1) != length(g2)){
    return FALSE"
>   ##then compare each vertix and its related edges and weights
>   } else {
Error: unexpected '}' in "  }"
>     for(i in 1:length(g1)){
+       for(j in 1:length(g2)){
+         if(names(g1[i]) == names(g2[j]){
Error: unexpected '{' in:
"      for(j in 1:length(g2)){
        if(names(g1[i]) == names(g2[j]){"
>           flag = 1
>           if(g1[[i]]$edges != g2[[j]]$edges){
+               return FALSE            
Error: unexpected numeric constant in:
"          if(g1[[i]]$edges != g2[[j]]$edges){
              return FALSE"
>           } else {
Error: unexpected '}' in "          }"
>             if(g1[[i]]$weights != g2[[j]]$weights){
+               return FALSE
Error: unexpected numeric constant in:
"            if(g1[[i]]$weights != g2[[j]]$weights){
              return FALSE"
>             } else {
Error: unexpected '}' in "            }"
>               return TRUE
Error: unexpected numeric constant in "              return TRUE"
>             }
Error: unexpected '}' in "            }"
>           }
Error: unexpected '}' in "          }"
>         }   
Error: unexpected '}' in "        }"
>       }
Error: unexpected '}' in "      }"
>       
>       if(flag != 1){
+         return FALSE
Error: unexpected numeric constant in:
"      if(flag != 1){
        return FALSE"
>       }
Error: unexpected '}' in "      }"
>     }    
Error: unexpected '}' in "    }"
>   }  
Error: unexpected '}' in "  }"
> }
Error: unexpected '}' in "}"

1 个答案:

答案 0 :(得分:0)

大吃一惊并阅读R的简介以理解语法。部分问题是'return'是一个函数调用,你必须得到parends中的值。

同时获得一个文本编辑器,突出显示了parends和bracket,因为你错过了一些。这是一个有效的函数:

> isomorphic <- function(g1,g2){
+   ##first calculate number of vertices, if not equal, return false
+   if(length(g1) != length(g2)){
+     return( FALSE)
+   ##then compare each vertix and its related edges and weights
+   } else {
+     for(i in 1:length(g1)){
+       for(j in 1:length(g2)){
+         if(names(g1[i]) == names(g2[j])){
+           flag = 1
+           if(g1[[i]]$edges != g2[[j]]$edges){
+               return( FALSE            )
+           } else {
+             if(g1[[i]]$weights != g2[[j]]$weights){
+               return (FALSE)
+             } else {
+               return( TRUE)
+             }
+           }
+         }   
+       }
+ 
+       if(flag != 1){
+         return( FALSE)
+       }
+     }    
+   }  
+ }
>